Java swing to achieve a simple picture display (test to generate picture shortcuts)

Source: Internet
Author: User
Package com.ts.x;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.io.ByteArrayOutputStream;
Import Java.io.InputStream;
Import Java.net.URL;

Import java.util.Hashtable;
Import Javax.swing.Icon;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;

Import Javax.swing.JLabel;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatWriter;

Import Com.google.zxing.common.BitMatrix;
	public class Swingtestimg extends jframe{private static final long serialversionuid = 1L;
	Private JLabel label;
	private icon icon;
	private image Image;
	public static void Main (string[] args) {new swingtestimg ();
			Public swingtestimg () {try{settitle ("Test picture simple display");
			SetSize (300,300);
			Setdefaultcloseoperation (Dispose_on_close);
			label = new JLabel ();
			Add (label);
			SetVisible (TRUE);
			A: Web URL picture//icon = new ImageIcon ("HTTP://TP1.SINAIMG.CN/3223061260/180/5659068018/1"); B: The project directory under the picture
InputStream is = SwingTestImg.class.getResourceAsStream ("Twodimensioncode.gif");
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
byte [] buff = new byte[100];
int readcount = 0; while ((Readcount = Is.read (buff,0,100)) > 0) {//Baos.write (Buff,0,readcount);//}/byte [] inbyte = Bao
S.tobytearray ();
icon = new ImageIcon (inbyte);
			C: Local Disk picture, the picture is too large, will result in blank display//image = new ImageIcon ("D:/1.png"). GetImage ();
			D: Code generated bufferedimage two-dimensional code picture Hashtable hints = new Hashtable ();
			Hints.put (Encodehinttype.character_set, "UTF-8");
			Bitmatrix matrix = new Multiformatwriter (). Encode ("http://www.vcspark.com/", Barcodeformat.qr_code, and 300,hints);
			int width = matrix.getwidth ();
			int height = matrix.getheight ();
			BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb); for (int x = 0;x < width. x + +) {for (int y = 0;y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)? 0xff00 0000:0xffFFFFFF);
		} icon = new ImageIcon (image);
			}catch (Exception e) {System.out.println ("Initialization Failed" +e.getmessage ());
		E.printstacktrace ();
} label.seticon (icon);
	Label.seticon (new ImageIcon (image));
 } 
}


Test two-dimensional code Generation results:

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.