Create and parse a QR code with a logo

Source: Internet
Author: User

Decoderqrcode is used to parse the QR code with a logo. If an error is reported and the Code cannot be parsed, the system queries the information and finds a more powerful QR code.

Tool: goolezxing

 

First, download the jar package of goolezxing.

---------------------------------------- Generate a QR code ------------------------------------------------

Public class goolezxingqrcodeutil {


Private Static final string charset = "UTF-8 ";
Private Static final string format_name = "jpg ";
// QR code size
Private Static final int qrcode_size = 300;
// Logo width
Private Static final int width = 60;
// Logo height
Private Static final int Height = 60;

Private Static bufferedimage createimage (string content, string imgpath,
Boolean needcompress) throws exception {
Hashtable <encodehinttype, Object> hints = new hashtable <encodehinttype, Object> ();
Hints. Put (encodehinttype. error_correction, errorcorrectionlevel. H );
Hints. Put (encodehinttype. character_set, charset );
// Hints. Put (encodehinttype. margin, 1 );
Bitmatrix = new multiformatwriter (). encode (content,
Barcodeformat. qr_code, qrcode_size, qrcode_size, hints );
Int width = bitmatrix. getwidth ();
Int Height = bitmatrix. getheight ();
Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );
For (INT x = 0; x <width; X ++ ){
For (INT y = 0; y Image. setrgb (X, Y, bitmatrix. Get (x, y )? 0xff000000
: 0 xffffffff );
}
}
If (imgpath = NULL | "". Equals (imgpath )){
Return image;
}
// Insert an image
Goolezxingqrcodeutil. insertimage (image, imgpath, needcompress );
Return image;
}

/**
* Insert a logo
*
* @ Param Source
* QR code Image
* @ Param imgpath
* Logo image address
* @ Param needcompress
* Compression or not
* @ Throws exception
*/
Private Static void insertimage (bufferedimage source, string imgpath,
Boolean needcompress) throws exception {
File file = new file (imgpath );
If (! File. exists ()){
System. Err. println ("" + imgpath + "this file does not exist! ");
Return;
}
Image src = ImageIO. Read (new file (imgpath ));
Int width = SRC. getwidth (null );
Int Height = SRC. getheight (null );
If (needcompress) {// compress logo
If (width> width ){
Width = width;
}
If (height> height ){
Height = height;
}
Image image = SRC. getscaledinstance (width, height,
Image. scale_smooth );
Bufferedimage tag = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );
Graphics G = tag. getgraphics ();
G. drawimage (image, 0, 0, null); // draw the reduced graph.
G. Dispose ();
Src = image;
}
// Insert the logo
Graphics2d graph = source. creategraphics ();
Int x = (qrcode_size-width)/2;
Int y = (qrcode_size-height)/2;
Graph. drawimage (SRC, X, Y, width, height, null );
Shape shape = new roundrectangle2d. Float (X, Y, width, width, 6, 6 );
Graph. setstroke (New basicstroke (3f ));
Graph. Draw (SHAPE );
Graph. Dispose ();
}

/**
* Generate a QR code (embedded logo)
*
* @ Param content
* Content
* @ Param imgpath
* Logo address
* @ Param destpath
* Storage directory
* @ Param needcompress
* Whether to compress the logo
* @ Throws exception
*/
Public static void encode (string content, string imgpath, string destpath,
Boolean needcompress) throws exception {
Bufferedimage image = goolezxingqrcodeutil. createimage (content, imgpath,
Needcompress );
Mkdirs (destpath );
String file = new random (). nextint (99999999) + ". jpg ";
ImageIO. Write (image, format_name, new file (destpath + "/" + file ));
}

/**
* When the folder does not exist, mkdirs automatically creates a multi-level Directory, which is different from mkdir. (mkdir throws an exception if the parent directory does not exist)
* @ Author lanyuan
* Email: [email protected]
* @ Date 2013-12-11 10:16:36 AM
* @ Param destpath: storage directory
*/
Public static void mkdirs (string destpath ){
File file = new file (destpath );
// When the folder does not exist, mkdirs automatically creates a multi-level Directory, which is different from mkdir. (mkdir throws an exception if the parent directory does not exist)
If (! File. exists ()&&! File. isdirectory ()){
File. mkdirs ();
}
}

/**
* Generate a QR code (embedded logo)
*
* @ Param content
* Content
* @ Param imgpath
* Logo address
* @ Param destpath
* Storage address
* @ Throws exception
*/
Public static void encode (string content, string imgpath, string destpath)
Throws exception {
Goolezxingqrcodeutil. encode (content, imgpath, destpath, false );
}

/**
* Generate a QR code
*
* @ Param content
* Content
* @ Param destpath
* Storage address
* @ Param needcompress
* Whether to compress the logo
* @ Throws exception
*/
Public static void encode (string content, string destpath,
Boolean needcompress) throws exception {
Goolezxingqrcodeutil. encode (content, null, destpath, needcompress );
}

/**
* Generate a QR code
*
* @ Param content
* Content
* @ Param destpath
* Storage address
* @ Throws exception
*/
Public static void encode (string content, string destpath) throws exception {
Goolezxingqrcodeutil. encode (content, null, destpath, false );
}

/**
* Generate a QR code (embedded logo)
*
* @ Param content
* Content
* @ Param imgpath
* Logo address
* @ Param output
* Output stream
* @ Param needcompress
* Whether to compress the logo
* @ Throws exception
*/
Public static void encode (string content, string imgpath,
Outputstream output, Boolean needcompress) throws exception {
Bufferedimage image = goolezxingqrcodeutil. createimage (content, imgpath,
Needcompress );
ImageIO. Write (image, format_name, output );
}

/**
* Generate a QR code
*
* @ Param content
* Content
* @ Param output
* Output stream
* @ Throws exception
*/
Public static void encode (string content, outputstream output)
Throws exception {
Goolezxingqrcodeutil. encode (content, null, output, false );
}

/**
* Parse the QR code
*
* @ Param File
* QR code Image
* @ Return
* @ Throws exception
*/
Public static string decode (File file) throws exception {
Bufferedimage image;
Image = ImageIO. Read (File );
If (image = NULL ){
Return NULL;
}
Bufferedimageluminancesource source = new bufferedimageluminancesource (
Image );
Binarybitmap bitmap = new binarybitmap (New hybridbinarizer (source ));
Result result;
Hashtable <decodehinttype, Object> hints = new hashtable <decodehinttype, Object> ();
Hints. Put (decodehinttype. character_set, charset );
Result = new multiformatreader (). Decode (bitmap, hints );
String resultstr = result. gettext ();
Return resultstr;
}

/**
* Parse the QR code
*
* @ Param path
* QR code image address
* @ Return
* @ Throws exception
*/
Public static string decode (string path) throws exception {
Return goolezxingqrcodeutil. Decode (new file (PATH ));
}

Public static void main (string [] ARGs) throws exception {
// String text = "I'm a Shiny boy !! ";
// Goolezxingqrcodeutil. encode (text, "F:/test/twocode/123.jpg"," F:/test/twocode/", true );


String imgpath = "F:/test/twocode/girl.png ";
String rs = goolezxingqrcodeutil. Decode (imgpath );
System. Out. println ("Resolution result =" + "\ n" + RS );

}
}

 

 

 

--------------------------------------- Parse the QR code (with logo images )-------------------------------------

Public class bufferedimageluminancesource extends luminancesource {
Private Final bufferedimage image;
Private Final int left;
Private Final int top;

Public bufferedimageluminancesource (bufferedimage image ){
This (image, 0, 0, image. getwidth (), image. getheight ());
}

Public bufferedimageluminancesource (bufferedimage image, int left,
Int top, int width, int height ){
Super (width, height );

Int sourcewidth = image. getwidth ();
Int sourceheight = image. getheight ();
If (left + width> sourcewidth | top + height> sourceheight ){
Throw new illegalargumentexception (
"Crop rectangle does not fit within image data .");
}

For (INT y = top; y <top + height; y ++ ){
For (INT x = left; x <left + width; X ++ ){
If (image. getrgb (x, y) & 0xff000000) = 0 ){
Image. setrgb (X, Y, 0 xffffff); // = white
}
}
}

This. Image = new bufferedimage (sourcewidth, sourceheight,
Bufferedimage. type_byte_gray );
This. image. getgraphics (). drawimage (image, 0, 0, null );
This. Left = left;
This. Top = top;
}


Public byte [] getrow (INT y, byte [] row ){
If (Y <0 | Y> = getheight ()){
Throw new illegalargumentexception (
"Requested row is outside the image:" + y );
}
Int width = getwidth ();
If (ROW = NULL | row. Length <width ){
Row = new byte [width];
}
Image. getraster (). getdataelements (left, top + Y, width, 1, row );
Return row;
}


Public byte [] getmatrix (){
Int width = getwidth ();
Int Height = getheight ();
Int area = width * height;
Byte [] matrix = new byte [area];
Image. getraster (). getdataelements (left, top, width, height, matrix );
Return matrix;
}


Public Boolean iscropsupported (){
Return true;
}


Public luminancesource crop (INT left, int top, int width, int height ){
Return new bufferedimageluminancesource (image, this. Left + left,
This. Top + top, width, height );
}


Public Boolean isrotatesupported (){
Return true;
}


Public luminancesource rotatecounterclockwise (){
Int sourcewidth = image. getwidth ();
Int sourceheight = image. getheight ();
Affinetransform transform = new affinetransform (0.0,-1.0, 1.0,
0.0, 0.0, sourcewidth );
Bufferedimage rotatedimage = new bufferedimage (sourceheight,
Sourcewidth, bufferedimage. type_byte_gray );
Graphics2d G = rotatedimage. creategraphics ();
G. drawimage (image, transform, null );
G. Dispose ();
Int width = getwidth ();
Return new bufferedimageluminancesource (rotatedimage, top,
Sourcewidth-(left + width), getheight (), width );
}
}

 

 

So far, the demonstration has been completed by using zxing to generate and parse the QR code, mainly for self-preparation, and at the same time convenient for people in need. Haha!

Create and parse a QR code with a logo

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.