Zxing generates QR code

Source: Internet
Author: User

Package com. zxing;
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. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. util. hashtable;

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 qrcode {
// Average Image Width
Private Static final int image_width = 25;
Private Static final int image_height = 25;
Private Static final int image_half_width = image_width/2;
Private Static final int frame_width = 2;

// QR code
Private Static multiformatwriter mutiwriter = new multiformatwriter ();

/**
*
* @ Param content the text displayed by the QR code
* @ Param width the width of the QR code
* @ Param height the height of the QR code
* @ Param srcimagepath: Nested image in the middle
* @ Param destimagepath address generated by the QR code
*/
Public static void encode (string content, int width, int height,
String srcimagepath, outputstream ){
Try {
// ImageIO. Write parameter 1, bufferedimage 2, output format 3, output file
ImageIO. Write (genbarcode (content, width, height, srcimagepath), "jpg", outputstream );

} Catch (ioexception e ){
E. printstacktrace ();
} Catch (writerexception e ){
E. printstacktrace ();
}
}

/**
* Get bufferedimage
*
* @ Param content the text displayed by the QR code
* @ Param width the width of the QR code
* @ Param height the height of the QR code
* @ Param srcimagepath: Nested image in the middle
* @ Return
* @ Throws writerexception
* @ Throws ioexception
*/
Private Static bufferedimage genbarcode (string content, int width, int height, string srcimagepath)
Throws writerexception, ioexception {
// Read the source Image
Bufferedimage scaleimage = Scale (srcimagepath, image_width,
Image_height, false );

Int [] [] srcpixels = new int [image_width] [image_height];
For (INT I = 0; I <scaleimage. getwidth (); I ++ ){
For (Int J = 0; j <scaleimage. getheight (); j ++ ){
Srcpixels [I] [J] = scaleimage. getrgb (I, j );
}
}

Hashtable hint = new hashtable ();
Hint. Put (encodehinttype. character_set, "UTF-8 ");
Hint. Put (encodehinttype. error_correction, errorcorrectionlevel. H );
// Generate a QR code
Bitmatrix matrix = mutiwriter. encode (content, barcodeformat. qr_code,
Width, height, hint );

// Convert the two-dimensional matrix into a one-dimensional pixel array
Int halfw = matrix. getwidth ()/2;
Int halfh = matrix. getheight ()/2;
Int [] pixels = new int [width * Height];

// System. Out. println (matrix. getheight ());
For (INT y = 0; y <matrix. getheight (); y ++ ){
For (INT x = 0; x <matrix. getwidth (); X ++ ){
// Read the image
If (x> halfw-image_half_width
& X & Y> halfh-image_half_width
& Y Pixels [y * width + x] = srcpixels [X-halfw
+ Image_half_width] [Y-halfh + image_half_width];
}
// Create a border around the image
Else if (x> halfw-image_half_width-frame_width
& X & Y> halfh-image_half_width-frame_width & Y + Image_half_width + frame_width)
| (X> halfw + image_half_width-frame_width
& X & Y> halfh-image_half_width-frame_width & Y + Image_half_width + frame_width)
| (X> halfw-image_half_width-frame_width
& X & Y> halfh-image_half_width-frame_width & Y -Image_half_width + frame_width)
| (X> halfw-image_half_width-frame_width
& X & Y> halfh + image_half_width-frame_width & Y + Image_half_width + frame_width )){
Pixels [y * width + x] = 0 xfffffff;
} Else {
// The Color of the QR code can be modified here, and the color of the QR code and background can be determined separately;
Pixels [y * width + x] = matrix. Get (x, y )? 0xff000000
: 0 xfffffff;
}
}
}

Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );
Image. getraster (). setdataelements (0, 0, width, height, pixels );

Return image;
}

/**
* Scale the input original image by height and width to generate the required icons.
*
* @ Param srcimagefile
* Source file address
* @ Param height
* Target Height
* @ Param width
* Target width
* @ Param hasfiller
* If the proportion is incorrect, do you need to fill the White List? True indicates the White List; false indicates that the White List is not filled;
* @ Throws ioexception
*/
Private Static bufferedimage Scale (string srcimagefile, int height,
Int width, Boolean hasfiller) throws ioexception {
Double ratio = 0.0; // scaling ratio
File file = new file (srcimagefile );
Bufferedimage srcimage = ImageIO. Read (File );
Image destimage = srcimage. getscaledinstance (width, height,
Bufferedimage. scale_smooth );
// Calculate the proportion
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) {// whitelist
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 (null)/2, destimage. getwidth (null ),
Destimage. getheight (null), color. White, null );
Else
Graphic. drawimage (destimage,
(Width-destimage. getwidth (null)/2, 0, destimage
. Getwidth (null), destimage. getheight (null ),
Color. White, null );
Graphic. Dispose ();
Destimage = image;
}
Return (bufferedimage) destimage;
}

Public static void main (string [] ARGs ){
File file = new file ("D: // androidqr.jpg ");

Outputstream out = NULL;
Try {
Out = new fileoutputstream (File );
} Catch (filenotfoundexception e ){
E. printstacktrace ();
}

Qrcode. encode ("xx.apk ",
350,350, "d:/android.jpg", out );
}
}

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.