Java generated two-dimensional code there are many development of jar packages such as zxing is Google developed, here I use the zxing development package to achieve. In many projects we need to dynamically generate two-dimensional code, to provide users, so that more people can be very good through the two-dimensional code to experience their applications.
The following post code, has been tested through, you can directly copy the code to use:
Final implementation results:
Java generated two-dimensional code
The code is as follows:
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.geom.AffineTransform;
Import java.awt.image.AffineTransformOp;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import Java.util.HashMap;
Import Java.util.Map;
Import Javax.imageio.ImageIO;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatWriter;
Import com.google.zxing.WriterException;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class Matrixtoimagewriter {private static final int image_width = 100;
private static final int image_height = 100;
private static final int image_half_width = IMAGE_WIDTH/2;
private static final int frame_width = 2;
private static Multiformatwriter Mutiwriter = new Multiformatwriter (); public static void Encode (String content, int width,int height, string srcimagepath, string Destimagepath {try {imageio.write Genbarcode (content, width, he
ight, Srcimagepath), "JPG", new File (Destimagepath));
catch (IOException e) {e.printstacktrace ();
catch (Writerexception e) {e.printstacktrace (); } private static BufferedImage Genbarcode (string content, int width, int height, String srcimagepath) throws Writerexception, IOException {bufferedimage scaleimage = scale (Srcimagepath, Image_width, Image_height
, true);
int[][] Srcpixels = new Int[image_width][image_height]; for (int i = 0; i < scaleimage.getwidth (); i++) {for (int j = 0; J < Scaleimage.getheight (); j + +) {s
RCPIXELS[I][J] = Scaleimage.getrgb (i, j);
} map<encodehinttype, object> hint = new Hashmap<encodehinttype, object> ();
Hint.put (Encodehinttype.character_set, "utf-8"); Hint.put (Encodehinttype.error_correction, ErrorcorRectionLevel.H);
Generate two-dimensional code Bitmatrix matrix = mutiwriter.encode (content, Barcodeformat.qr_code, width, height, hint);
The two-dimensional matrix is converted into one dimensional pixel Group int halfw = Matrix.getwidth ()/2;
int halfh = Matrix.getheight ()/2;
int[] pixels = new int[width * height]; for (int y = 0; y < matrix.getheight (); y++) {for (int x = 0; x < matrix.getwidth (); Adjust color range and color according to your needs if (x > 0 && x < 170 && y > 0 && y < 170) {color
color = new Color (231, 144, 56);
int colorint = Color.getrgb (); Pixels[y * width + x] = Matrix.get (x, y)?
colorint:16777215;
//Read the picture else if (x > Halfw-image_half_width && x < HALFW + Image_half_width
&& y > Halfh-image_half_width && y < HALFH + image_half_width) { Pixels[y * width + x] = SRCPIXELS[X-HALFW + IMAGE_HALF_WIDTH][Y-HALFH + image_half_width]; else if (x > Halfw-image_half_width-frame_width && x < Halfw-image_half_width + Frame_w Idth && y > Halfh-image_half_width-frame_width && y < HALFH + image_half _width + frame_width) | |
(x > HALFW + image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > Halfw-image_half_width-frame_width && y < HALFH + Image_hal F_width + frame_width) | |
(x > Halfw-image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > Halfh-image_half_width-frame_width && y < Halfh-image_hal F_width + frame_width) | | (x > Halfw-image_half_width-frame_width && x < HALFW + Image_haLf_width + frame_width && y > halfh + image_half_width-frame_width && y < HALFH
+ Image_half_width + frame_width) {Pixels[y * WIDTH + x] = 0XFFFFFFF;
In the picture four weeks to form a border} else {//two D code color int num1 = (int) (50.0-13.0)/Matrix.getheight ()
* (y + 1));
int num2 = (int) (165-(165.0-72.0)/Matrix.getheight () * (y + 1));
int num3 = (int) (162-(162.0-107.0)/Matrix.getheight () * (y + 1));
Color color = new Color (NUM1, num2, num3);
int colorint = Color.getrgb (); Here you can modify the color of two-dimensional code, you can set the color of two-dimensional code and background respectively; Pixels[y * width + x] = Matrix.get (x, y)?
colorint:16777215; 0X000000:0XFFFFFF}} bufferedimage image = New BufferedImage (width, height, buffere
DIMAGE.TYPE_INT_RGB); Image.getraster (). setDataElements (0, 0, width, height, pixels);
return image; private static BufferedImage scale (String srcimagefile, int height, int width, Boolean hasfiller) throws Ioexc
Eption {double ratio = 0.0;//Scaling File = new file (srcimagefile);
BufferedImage srcimage = imageio.read (file);
Image destimage = srcimage.getscaledinstance (width, height, bufferedimage.scale_smooth); Calculate scale if (srcimage.getheight () > height) | | (Srcimage.getwidth () > width))
{if (Srcimage.getheight () > Srcimage.getwidth ()) {ratio = (new Integer (height)). Doublevalue ()
/Srcimage.getheight ();
else {ratio = (new Integer (width)). Doublevalue ()/srcimage.getwidth ();
} affinetransformop op = new Affinetransformop (affinetransform.getscaleinstance (ratio, ratio), null);
Destimage = Op.filter (srcimage, NULL);
} if (Hasfiller) {//filler bufferedimage image = new BufferedImage (width, height, BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D graphic = Image.creategraphics ();
Graphic.setcolor (Color.White);
Graphic.fillrect (0, 0, width, height); if (width = = destimage.getwidth (null)) Graphic.drawimage (destimage, 0, Height-destimage.getheight (n
ull))/2, destimage.getwidth (null), destimage.getheight (null), color.white, NULL); else Graphic.drawimage (Destimage, (Width-destimage.getwidth (NULL))/2, 0, Destimage.get
Width (NULL), destimage.getheight (null), color.white, NULL);
Graphic.dispose ();
Destimage = image;
Return (bufferedimage) destimage;
public static void Main (string[] args) throws Unsupportedencodingexception {//sequential content (not supported in Chinese), wide, long, middle icon path, store path
Matrixtoimagewriter.encode ("http://www.baidu.com/", ",", "D:\\logo.png", "d:\\2013-01.jpg"); }
}
The above is the entire content of this article, to help you design their own two-dimensional code.