Android zxing Two-dimensional Code classic case sharing _android

Source: Internet
Author: User
Tags gettext int size

The example of this article for us to analyze the zxing generation of two-dimensional code of the classic case for your reference, the specific contents are as follows

1, first of all, compile compile ' com.google.zxing:core:3.2.1 '

2. actual combat

public class QRCode {private static int image_halfwidth = 50;//width value, affect middle picture size/** * Generate two-dimensional code, the default size is 500*500 * * @return bit

Map */public static Bitmap Createqrcode () {return Createqrcode ();} /** * Generates two-dimensional code, the default size is 500*500 * * @param text needs to generate two-dimensional code text, URL, etc. * @return Bitmap/public static bitmap Createqrcode (String

Text) {return Createqrcode (text, 500);} /** * Generate Two-dimensional code * * @param text needs to generate two-dimensional code text, URL, etc. * @param size needs to generate a two-dimensional code () * @return Bitmap/public static bitmap creat
  Eqrcode (String text, int size) {try {hashtable<encodehinttype, string> hints = new hashtable<> ();
  Hints.put (Encodehinttype.character_set, "utf-8");
  Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (text, barcodeformat.qr_code, size, size, hints);
  int[] pixels = new int[size * size]; for (int y = 0; y < size; y++) {for (int x = 0; x < size; x +) {if (Bitmatrix.get (y)) {Pixels[y *
    Size + x] = 0xff000000;
    else {pixels[y * size + x] = 0xFFFFFFFF;

}   } Bitmap Bitmap = bitmap.createbitmap (size, size, Bitmap.Config.ARGB_8888);
  Bitmap.setpixels (pixels, 0, size, 0, 0, size, size);
 return bitmap;
  catch (Writerexception e) {e.printstacktrace ();
 return null; }/** * Generates a two-dimensional code with a logo, the default two-dimensional code size of 500,logo for two-dimensional code 1/5 * * @param text needs to generate two-dimensional code text, URL, etc. * @param mbitmap logo file * @return bit Map */public static Bitmap Createqrcodewithlogo (String text, Bitmap mbitmap) {return Createqrcodewithlogo (Text,500,mbi
TMAP); 
 /** * Generate a two-dimensional code with a logo, the logo defaults to two-dimensional code 1/5 * * @param text needs to generate two-dimensional code text, URL, etc. * @param size needs to generate two-dimensional code sizes () * @param mbitmap logo file * @return Bitmap */public static bitmap Createqrcodewithlogo (String text, int size, bitmap mbitmap) {try {Image_h
  Alfwidth = SIZE/10;
  Hashtable<encodehinttype, object> hints = new hashtable<> ();
  Hints.put (Encodehinttype.character_set, "utf-8"); * * Set the Fault tolerance level, the default is ERRORCORRECTIONLEVEL.L * because the middle of the logo so we recommend that you adjust the fault-tolerant level to H, otherwise it may appear not recognized * * HINTS.PUT (encodehinttype.error_c ORREction, ErrorCorrectionLevel.H);

  Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (text, barcodeformat.qr_code, size, size, hints);
  int width = bitmatrix.getwidth ();//matrix height int height = bitmatrix.getheight ();//matrix width int HALFW = WIDTH/2;

  int halfh = HEIGHT/2;
  Matrix M = new Matrix ();
  float SX = (float) 2 * image_halfwidth/mbitmap.getwidth ();
  Float sy = (float) 2 * image_halfwidth/mbitmap.getheight ();
  M.setscale (SX, SY); Set the zoom information//Set the logo picture by Martix information to scale Mbitmap = Bitmap.createbitmap (mbitmap, 0, 0, mbitmap.getwidth (), Mbitmap.getheigh

  T (), M, false);
  int[] pixels = new int[size * size]; for (int y = 0; y < size, y++) {for (int x = 0; x < size; x +) {if (× > Halfw-image_halfwidth &&am P X < HALFW + image_halfwidth && y > Halfh-image_halfwidth && y < HALFH + IMAGE_HALFW Idth) {//This location is used for storing picture information//recording pictures per pixel information pixels[y * width + x] = Mbitmap.getpixel (X-HALFW + image_Halfwidth, Y-HALFH + image_halfwidth);
     else {if (Bitmatrix.get (x, y)) {pixels[y * size + x] = 0xff000000;
     else {pixels[y * size + x] = 0xFFFFFFFF;
  }}} Bitmap Bitmap = bitmap.createbitmap (size, size, Bitmap.Config.ARGB_8888);
  Bitmap.setpixels (pixels, 0, size, 0, 0, size, size);
 return bitmap;
  catch (Writerexception e) {e.printstacktrace ();
 return null;

 }
}

3, analysis

Generate two-dimensional code, the above can do the operation generated with text and URLs, with pictures and two-dimensional code size, in the code have a detailed summary.

4, the two-dimensional code to enlarge and reduce the operation

public class Mainactivity extends Appcompatactivity {Bitmap bp = null;
 ImageView ImageView;
 float ScaleWidth;

 float ScaleHeight;
 int h;
 Boolean num = false;

Private Bitmap netfits;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);


 Setcontentview (R.layout.activity_main);

 Displaymetrics dm = new Displaymetrics ()///Create Matrix Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
 ImageView = (ImageView) Findviewbyid (R.id.imageview);


 bp= Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);

 Netfits = Qrcode.createqrcodewithlogo ("Zhang", 200,bp);
 int width = netfits.getwidth ();
 int height = netfits.getheight (); int w = dm.widthpixels; Get the width of the screen final int h = dm.heightpixels;
 Get the height of the screen ScaleWidth = ((float) w)/width;
 ScaleHeight = ((float) w)/height;

 Imageview.setimagebitmap (netfits); Imageview.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {if (num = = Tru
E) {    Matrix matrix = new Matrix ();

    Matrix.postscale (ScaleWidth, ScaleHeight);
    Bitmap Newbitmap = Bitmap.createbitmap (netfits, 0, 0, netfits.getwidth (), Netfits.getheight (), Matrix, True);
    Imageview.setimagebitmap (NEWBITMAP);
   num = false;
    else {Matrix matrix = new Matrix ();
    Matrix.postscale (1.0f, 1.0f);
    Bitmap Newbitmap = Bitmap.createbitmap (netfits, 0, 0, netfits.getwidth (), Netfits.getheight (), Matrix, True);
    Imageview.setimagebitmap (NEWBITMAP);
   num = true;
}
  }
 });

 }

In normal mode we can handle it ourselves:

@Override Public
Boolean ontouchevent (Motionevent event) {

 switch (event.getaction ()) {

  case Motionevent.action_down://When the screen detects that the first contact is pressed, the event is triggered.
   if (num = = true) {
    matrix matrix = new Matrix ();
    Matrix.postscale (ScaleWidth, scaleheight);

    Bitmap Newbitmap = Bitmap.createbitmap (netfits, 0, 0, netfits.getwidth (), Netfits.getheight (), Matrix, true);
    Imageview.setimagebitmap (NEWBITMAP);
    num = false;
   } else {
    Matrix matrix = new Matrix ();
    Matrix.postscale (1.0f, 1.0f);
    Bitmap Newbitmap = Bitmap.createbitmap (netfits, 0, 0, netfits.getwidth (), Netfits.getheight (), Matrix, true);
    Imageview.setimagebitmap (NEWBITMAP);
    num = true;
   }
   break;


 Return Super.ontouchevent (event);
}

Add feature: See if selected, add picture to two-dimensional code

private void Initview () {
 edittext = (edittext) This.findviewbyid (r.id.qrcode_et);
 Button = (button) This.findviewbyid (R.ID.QRCODE_BT);
 ImageView = (ImageView) This.findviewbyid (R.ID.QRCODE_IV);
 CB = (CheckBox) This.findviewbyid (R.ID.QRCODE_CB);

 Button.setonclicklistener (New View.onclicklistener () {
  @Override public
  void OnClick (view view) {
   if ( Cb.ischecked ()) {
    Bitmap Bitmap = Qrcode.createqrcodewithlogo (Edittext.gettext (). toString (),
      Bitmapfactory.decoderesource (Getresources (), R.drawable.logo_me));
    Imageview.setimagebitmap (bitmap);
   } else{
    Bitmap Bitmap = Qrcode.createqrcode (Edittext.gettext (). toString ();
    Imageview.setimagebitmap (bitmap);}}
 );


Above is a simple implementation of two-dimensional code and narrowing the method, if better implementation, welcome to communicate!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.