Android QR code project-Implementation of QQ-like QR code

Source: Internet
Author: User

Android QR code project-Implementation of QQ-like QR code

The QR code has been developed to almost every App. Previously, the QR code function was used in the project to study how to embed the zxing QR code project, the previous method contains the most basic QR code scanning capabilities. When using QQ, you can see the QQd scan. The function is relatively complete. You can scan pictures and turn on the flash, QR codes can also be generated, which are commonly used functions. As a result, like the QR code style and functions of QQ, I also made a common project, if you want to use the QR code in the future, you do not have to perform configuration and other work. You can directly associate it with this QR code project. at the end of the article, I will upload the entire project to my resources and download it if necessary.

 

Next let's take a look at the whole: click on the album:

The QR code generated after the light is turned on and the device serial number:

 

Without the help of an artist, the screen looks really inferior to that of QQ. Fortunately, the function is basically achieved. The album is to select the QR code scan from the local image library, and turn on the light to turn on the mobile phone flash, the QR code is used to generate a QR code for the mobile phone serial number. The following is the code section. comments are made to the changed and added parts. I believe you can understand them. '

1. Add code to the album function. The code is added to the CaptureActivity. java class of the zxing project.

Photo Album click event processing:

Public void onClick (View v) {switch (v. getId () {case R. id. photo_btn: // open the album Intent innerIntent = new Intent (Intent. ACTION_GET_CONTENT); // android. intent. action. GET_CONTENTinnerIntent.setType (image/*); Intent wrapperIntent = Intent. createChooser (innerIntent, select the QR code image); this. startActivityForResult (wrapperIntent, REQUEST_CODE); break ;}}
Use zxing to scan the image:
Protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (resultCode = RESULT_ OK) {switch (requestCode) {case REQUEST_CODE: // obtain the path of the selected image, Cursor cursor = getContentResolver (). query (data. getData (), null, null); if (cursor. moveToFirst () {photo_path = cursor. getString (cursor. getColumnIndex (MediaStore. images. media. DATA );} Cursor. close (); new Thread (new Runnable () {@ Overridepublic void run () {Result result = scanningImage (photo_path); if (result! = Null) {Message m = mHandler. obtainMessage (); m. what = PARSE_BARCODE_SUC; m. obj = result. getText (); mHandler. sendMessage (m);} else {Message m = mHandler. obtainMessage (); m. what = PARSE_BARCODE_FAIL; m. obj = Scan failed !; MHandler. sendMessage (m) ;}}). start (); break ;}}}
ScanneringImage () method code:
/*** Scan QR code image method ** @ param path * @ return */public Result scanningImage (String path) {if (TextUtils. isEmpty (path) {return null;} Hashtable
 
  
Hints = new Hashtable
  
   
(); Hints. put (DecodeHintType. CHARACTER_SET, UTF8); // sets the BitmapFactory encoding of the QR code. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; // first obtain the original size scanBitmap = BitmapFactory. decodeFile (path, options); options. inJustDecodeBounds = false; // get the new size int sampleSize = (int) (options. outHeight/(float) 200); if (sampleSize <= 0) sampleSize = 1; options. inSampleSize = sampleSize; scanBitmap = BitmapFactory. decodeFile (path, options); export source = new RGBLuminanceSource (scanBitmap); BinaryBitmap bitmap1 = new BinaryBitmap (new region (source); QRCodeReader reader = new QRCodeReader (); try {return reader. decode (bitmap1, hints);} catch (NotFoundException e) {e. printStackTrace ();} catch (ChecksumException e) {e. printStackTrace ();} catch (FormatException e) {e. printStackTrace ();} return null ;}
  
 
2. Enable the flashlight. below is the code to be added to the flashlight. The code is added to CameraManager. java.

 

 

/*** Turn on the flashlight by setting Camera */public void turnLightOn () {if (camera = null) {return;} Parameters parameters = camera. getParameters (); if (parameters = null) {return;} List
 
  
FlashModes = parameters. getSupportedFlashModes (); if (flashModes = null) {return;} String flashMode = parameters. getFlashMode (); Log. I (TAG, Flash mode: + flashMode); Log. I (TAG, Flash modes: + flashModes); // if (! Parameters. FLASH_MODE_TORCH.equals (flashMode) {// Turn on the flasffe (flashModes. contains (Parameters. FLASH_MODE_TORCH) {parameters. setFlashMode (Parameters. FLASH_MODE_TORCH); camera. setParameters (parameters); camera. startPreview ();} else {}}/ *** disable the flashlight by setting Camera * @ param mCamera */public void turnLightOff () {if (camera = null) {return;} Parameters parameters = camera. getParameters (); if (parameters = null) {return;} List
  
   
FlashModes = parameters. getSupportedFlashModes (); String flashMode = parameters. getFlashMode (); // Check if camera flash existsif (flashModes = null) {return;} // if (! Parameters. FLASH_MODE_OFF.equals (flashMode) {// Turn off the flasffe (flashModes. contains (Parameters. FLASH_MODE_OFF) {parameters. setFlashMode (Parameters. FLASH_MODE_OFF); camera. setParameters (parameters);} else {Log. e (TAG, FLASH_MODE_OFF not supported );}}}
  
 
3. Generate a QR code and add it to the CaptureActivity. java class.

 

 

/***** Obtain the serial number of the mobile phone device * @ param context * @ return */public static String getIMEI (Context context) {TelephonyManager tm = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE); // Get deviceIdString deviceId = tm. getDeviceId (); // If running on an emulatorif (deviceId = null | deviceId. trim (). length () = 0 | deviceId. matches (0 +) {deviceId = (new StringBuilder (EMU )). append (new Random (System. currentTimeMillis ())). nextLong ()). toString ();} return deviceId;}/*** QR code generation method */private Bitmap createQRCode () {int QR_WIDTH = 100; int QR_HEIGHT = 100; try {// core package QRCodeWriter writer = new QRCodeWriter (); String mime = getIMEI (this); if (text = null |. equals (text) | text. length () <1) {return null;} // convert the input text into a two-dimensional code BitMatrix martix = writer. encode (text, BarcodeFormat. QR_CODE, QR_WIDTH, QR_HEIGHT); System. out. println (w: + martix. getWidth () + h: + martix. getHeight (); Hashtable
 
  
Hints = new Hashtable
  
   
(); Hints. put (EncodeHintType. CHARACTER_SET, UTF-8); BitMatrix bitMatrix = new QRCodeWriter (). encode (text, BarcodeFormat. QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int [] pixels = new int [QR_WIDTH * QR_HEIGHT]; for (int y = 0; y <QR_HEIGHT; y ++) {for (int x = 0; x <QR_WIDTH; x ++) {if (bitMatrix. get (x, y) {pixels [y * QR_WIDTH + x] = 0xff000000;} else {pixels [y * QR_WIDTH + x] = 0 xffffffff ;}}} // cheng chen de er wei ma Bitmap bitmap = Bitmap. createBitmap (QR_WIDTH, QR_HEIGHT, Bitmap. config. ARGB_8888); bitmap. setPixels (pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); return bitmap;} catch (WriterException e) {e. printStackTrace ();} return null ;}
  
 

You can refer to this blog for the configuration of the initial project and the modification to the scan box. Www.bkjia.com

 

 



 

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.