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: