Zxing read-write QR code, desktop and mobile phone different usage

Source: Internet
Author: User
Tags gettext

Although Zxing is a barcode open source library implemented in Java, it does not mean that barcode applications implemented on the desktop can be used directly on the phone. Because many of the Java interfaces of Android are different. Here you share the different uses of Java barcode generation and reading.

Reference original: How to Write and Read the QR Code with ZXing in Java

Desmond Shaw

Translation: Yushulx

Get zxing Source code

The previous zxing was on Google Code, and now it's all moved to GitHub. Command line gets:

git clone https://github.com/zxing/zxing
Add zxing to your project

There are two ways to import zxing in your project:

    1. Compile the zxing into a jar package and import it into the project. For example, in Android Studio can create a new module, the zxing source code after the import can build a jar package.

    2. Direct use of zxing source code. Select Project Properties > Java Build Path > Source > Link sourcein the project attributes. Be sure to enter the correct folder name, or there will be a lot of package errors.

Zxing Source Code Analysis

To generate a QR code, you need to use the writer class. Search implements writer can see all zxing supported barcode writer.

All 1d/2d barcodes are included here. Zxing supported barcode types can be queried for Barcodeformat.java:

Public enum barcodeformat {   /** aztec 2d barcode format.  */  aztec,   /** codabar 1d format. */  codabar,    /** code 39 1d format. */  code_39,   /**  code 93 1d format. */  code_93,   /** code 128  1D format. */  CODE_128,   /** Data Matrix 2D  Barcode format. */  data_matrix,   /** ean-8 1d format.  */  ean_8,   /** ean-13 1d format. */  ean_13,    /** ITF  (interleaved two of five)  1d format. */   ITF,   /** MaxiCode 2D barcode format. */   Maxicode,   /** pdF417 format. */  pdf_417,   /** qr code 2d barcode  format. */  qr_code,   /** rss 14 */  rss_14,    /** rss expanded */  rss_expanded,   /** upc-a  1d format. */  upc_a,   /** upc-e 1d format. */   UPC_E,   /** UPC/EAN extension format. Not a  stand-alone format. */  upc_ean_extension }

multiformatwriter   This class covers a variety of barcode generators:

@Override   public bitmatrix encode (string contents,                            BarcodeFormat format,                           int width,  int height,                           map<encodehinttype,?> hints )  throws WriterException {     Writer writer;     switch  (format)  {      case EAN_8:         writer = new ean8writer ();         break;      case ean_13:        writer = new  Ean13writer ();         break;      case  upc_a:        writer = new upcawriter ();         break;      case QR_CODE:         writer = new qrcodewriter ();         break;      case CODE_39:         writer = new code39writer ();         break;      case CODE_128:         writer = new code128writer ();        break;     &nbsP; case itf:        writer = new itfwriter ();         break;      case pdf_417:         writer = new pdf417writer ();         break;      case CODABAR:         writer = new codabarwriter ();         break;      case DATA_MATRIX:         writer = new datamatrixwriter ();         break;      case AZTEC:         writer = new aztecwriter ();        break;      &nbsP;default:        throw new illegalargumentexception ("No  encoder available for format  " + format);    }     return writer.encode (contents, format, width, height, hints);   }

Similarly, you can search for implements Reader to see the corresponding class:

Using Multiformatreader can simplify your code.

Generation and reading of QR codes on Windows, Mac and Linux

Using Java to create a desktop barcode app, you need to use bufferedimage to manipulate the image.

Java QRCode Writer
Public static void writeqrcode ()  {    qrcodewriter writer =  new qrcodewriter ();    int width = 256, height =  256;    bufferedimage image = new bufferedimage (Width, height, &NBSP;BUFFEREDIMAGE.TYPE_INT_RGB); // create an empty image     int white = 255 << 16 | 255 << 8 | 255;     int black = 0;    try {         bitmatrix bitmatrix = writer.encode ("http://www.codepool.biz/ Zxing-write-read-qrcode.html ",  barcodeformat.qr_code, width, height);         for  (int i = 0; i < width; i++)  {             for  (int j = 0; j < height; j++ )  {                 Image.setrgb (I, j, bitmatrix.get (i, j)  ? black : white);  // set  pixel one by one            }         }         try  {            imageio.write (image,  "JPG" ,  new file ("Dynamsoftbarcode.jpg")); // save qr image to disk         } catch  (ioexception e)  {             // todo auto-generated catch block      &nBsp;      e.printstacktrace ();         }      } catch  (writerexception e)  {         // TODO Auto-generated catch block         e.printstacktrace ();     }}
Java QRCode Reader

When reading image data, it is necessary to use Rgbluminancesource to encapsulate the data RGB.

 public static string readqrcode (String filename)  {    file  file = new file (FileName);    bufferedimage image =  null;    binarybitmap bitmap = null;    result  result = null;     try {         image = imageio.read (file);         int[] pixels  = image.getrgb (0, 0, image.getwidth (),  image.getheight (), null, 0,  Image.getwidth ());        rgbluminancesource source =  New rgbluminancesource (Image.getwidth (),  image.getheight (),  pixels);         bitmap = new binarybitmap (New hybridbinarizer (source));     } catch&nbSP; (ioexception e)  {        // todo auto-generated  catch block        e.printstacktrace ();     }     if  (bitmap == null)          return null;     QRCodeReader reader = new  Qrcodereader ();       try {         result = reader.decode (bitmap);        return  Result.gettext ();    } catch  (notfoundexception e)  {         // TODO Auto-generated catch block         e.printstacktrace ();    } catch  (ChecksumException  e)  {        // todo auto-generated catch block         e.printstacktrace ();    } catch  (FormatException e)  {        // todo auto-generated catch block         e.printstacktrace ();    }      return null;}
Generation and reading of two-dimensional code on Android

On the Android platform, BufferedImage is not present. Instead, it was bitmap.

Android QRCode Writer
Qrcodewriter writer = new qrcodewriter (); try {    int width  = mimageview.getwidth ();     int height = mimageview.getheight ();     bitmatrix bitmatrix = writer.encode (Content, barcodeformat.qr_code ,  width, height);     bitmap bitmap = bitmap.createbitmap (width,  height, bitmap.config.argb_8888);    for  (int i = 0; i  < width; i++)  {        for  (int j  = 0; j < height; j++)  {             bitmap.setpixel (I, j, bitmatrix.get (i, j)  ? Color.BLACK:  color.white);        }    }     mimageview.setImagebitmap (bitmap);}  catch  (writerexception e)  {    e.printstacktrace ();}
Android QRCode Reader

The data that comes in from the camera preview is in NV21 format, so you need to use planaryuvluminancesource when encapsulating the data.

Multiformatreader reader = new multiformatreader ();             luminancesource source = new planaryuvluminancesource ( Yuvdata, datawidth, dataheight, left, top, width, height, false); Binarybitmap bitmap = new binarybitmap (New hybridbinarizer (source)); Result result;try {    result = reader.decode (bitmap);     if  (result != null)  {         Mdialog.settitle ("Result");         mdialog.setmessage (Result.getText () );         mdialog.show ();    }} catch  ( Notfoundexception e)  {    // todo auto-generated catch block     e.printstacktrace ();}


Zxing read-write QR code, desktop and mobile phone different usage

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.