Java Paste Picture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.