How do I get pictures from the Clipboard in the Java Swing program?
Here's a Java swing GUI I made:
Right-click on the menu to get the picture in the Clipboard by clicking "Paste Image" in the menu.
/*** * Paste the picture from the system Clipboard into the Swing label control * / Public void pasteclipboardimageaction() {BufferedImage bufferedimage=componentutil.getclipboardimage ( This. frame);if(Valuewidget.isnullorempty (BufferedImage)) {Guiutil23.alert ("There is no picture in the system Clipboard, please copy the picture first");return; } image=bufferedimage; Bytearrayoutputstream BAOs =NewBytearrayoutputstream ();Try{//Convert the pasted image to binary (byte array)Imageio.write (BufferedImage, Read_qrcode_from_pic_format/*jpg*/, BAOs); Qrbytes= Baos.tobytearray (); Componentutil.appendresult (Resultarea,"pasted QR code size: \ t"+qrbytes.length,true); }Catch(IOException e) {E.printstacktrace (); } Qrresultlabel.seticon (NewImageIcon (image)); }
Core approach: Parsing the contents of the Clipboard
public static BufferedImage Getclipboardimage (frame frame) {//Java. Lang. ClassCastException: Sun. AWT. DataTransfer. TransferableproxyCannot is cast to sun. AWT. DataTransfer. ClipboardtransferableTransferable Trans=frame. Gettoolkit(). Getsystemclipboard(). getcontents(NULL);BufferedImage Image=null;if (trans instanceof clipboardtransferable) {//Clipboardtransferable Clipboardtrans = (clipboardtransferable) TR Ans;try {if (null! = trans && trans. isdataflavorsupported(DataFlavor. Imageflavor) {Object Obj22=trans. Gettransferdata(DataFlavor. Imageflavor);if (! Valuewidget. IsNullOrEmpty(OBJ22)) {if (obj22 instanceof bufferedimage) {image= (bufferedimage) obj22;}}}} catch (Unsupportedflavorexception E1) {E1. Printstacktrace();GUIUtil23. Errordialog(E1);} catch (IOException E1) {E1. Printstacktrace();GUIUtil23. Errordialog(E1);} return image;}
Author: Good
Blog Address:
CSDN Blog Home
Iteye Blog Home
Java Paste Picture