-
Notifications
You must be signed in to change notification settings - Fork 79
/
ClipboardImage.java
103 lines (83 loc) · 2.33 KB
/
ClipboardImage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package mod.chiselsandbits.share.output;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import mod.chiselsandbits.blueprints.BlueprintData;
import mod.chiselsandbits.localization.LocalStrings;
import mod.chiselsandbits.localization.LocalizedMessage;
import mod.chiselsandbits.share.ScreenShotEncoder;
// this won't work, java refuses to support alpha channel on clipboard.
public class ClipboardImage implements IShareOutput
{
byte[] data = null;
public ClipboardImage()
{
}
@Override
public LocalizedMessage handleOutput(
final byte[] compressedData,
final BufferedImage screenshot ) throws UnsupportedEncodingException, IOException
{
BufferedImage image = ScreenShotEncoder.encodeScreenshot( screenshot, compressedData );
ImageTransferable transferable = new ImageTransferable( image );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents( transferable, null );
// save text version to game blueprint.
final StringBuilder o = new StringBuilder();
o.append( "[C&B](" );
o.append( Base64.getEncoder().encodeToString( compressedData ) );
o.append( ")[C&B]" );
data = o.toString().getBytes( "UTF-8" );
return new LocalizedMessage( LocalStrings.ShareClipboard );
}
static class ImageTransferable implements Transferable
{
private Image image;
public ImageTransferable(
Image image )
{
this.image = image;
}
public Object getTransferData(
DataFlavor flavor )
throws UnsupportedFlavorException
{
if ( isDataFlavorSupported( flavor ) )
{
return image;
}
else
{
throw new UnsupportedFlavorException( flavor );
}
}
public boolean isDataFlavorSupported(
DataFlavor flavor )
{
return flavor == DataFlavor.imageFlavor;
}
public DataFlavor[] getTransferDataFlavors()
{
return new DataFlavor[] { DataFlavor.imageFlavor };
}
}
@Override
public BlueprintData getData()
{
BlueprintData bpd = new BlueprintData( null );
try
{
bpd.loadData( new ByteArrayInputStream( data ) );
}
catch ( IOException e )
{
}
return bpd;
}
}