How to scan the QR code Zxing and zxing

Source: Internet
Author: User
Tags gety

How to scan the QR code Zxing and zxing
Dependency

Compile 'com. journeyapps: zxing-android-embedded: 3.3.0'Permission
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" />

MainActivity. java
Package com.bawei.com. myapplication; import android. content. intent; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. button; import android. widget. textView; import com. google. zxing. integration. android. intentIntegrator; import com. google. zxing. integration. android. intentResult; public class MainActivity extends AppCompatActivity {priva Te Button btnClick; private TextView tvResult; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); initEvent ();} private void initView () {btnClick = (Button) findViewById (R. id. btn_click); tvResult = (TextView) findViewById (R. id. TV _result);} private void initEvent () {btnClick. setOnClickListener (ne W View. onClickListener () {@ Override public void onClick (View v) {// if you want to use fragment to redirect to the interface, // IntentIntegrator intentIntegrator = IntentIntegrator. forsuppfrfragment (ShopFragment. this ). setCaptureActivity (CustomScanAct. class); IntentIntegrator intentIntegrator = new IntentIntegrator (MainActivity. this); intentIntegrator. setDesiredBarcodeFormats (IntentIntegrator. ALL_CODE_TYPES ). setPrompt ("add the QR code/barcode In the box, you can automatically scan ") // write the prompt. setOrientationLocked (false) // The Scanning direction is fixed. setCaptureActivity (CustomScanAct. class) // set the custom activity to CustomActivity. initiateScan (); // initialization scan});} // obtain the scan result @ Override public void onActivityResult (int requestCode, int resultCode, Intent data) {IntentResult intentResult = IntentIntegrator. parseActivityResult (requestCode, resultCode, data); if (intentResult! = Null) {if (intentResult. getContents () = null) {} else {// ScanResult is the obtained String ScanResult = intentResult. getContents (); tvResult. setText (ScanResult) ;}} else {super. onActivityResult (requestCode, resultCode, data );}}}
CustomViewfinderView. java
Package com.bawei.com. myapplication; import android. content. context; import android. graphics. canvas; import android. graphics. linearGradient; import android. graphics. rect; import android. graphics. shader; import android. util. attributeSet; import com. google. zxing. resultPoint; import com. journeyapps. barcode=. viewfinderView; import java. util. arrayList; import java. util. list;/*** Created by Wangrx on 2017/1 1/5. */public class CustomViewfinderView extends ViewfinderView {public int laserLinePosition = 0; public float [] position = new float [] {0f, 0.5f, 1f }; public int [] colors = new int [] {0x00ffffff, 0 xffffffff, 0x00ffffff}; public LinearGradient linearGradient; public CustomViewfinderView (Context context, AttributeSet attrs) {super (context, attrs);}/*** Override the draw method to draw your own scan box * @ param canvas */@ Override publi C void onDraw (Canvas canvas) {refreshSizes (); if (framingRect = null | response = null) {return;} Rect frame = framingRect; Rect previewFrame = previewFramingRect; int width = canvas. getWidth (); int height = canvas. getHeight (); // draw four corners of paint. setColor (0 xFFFFFFFF); // defines the paint brush color canvas. drawRect (frame. left, frame. top, frame. left + 70, frame. top + 10, paint); canvas. drawRect (frame. left, frame. t Op, frame. left + 10, frame. top + 70, paint); canvas. drawRect (frame. right-70, frame. top, frame. right, frame. top + 10, paint); canvas. drawRect (frame. right-10, frame. top, frame. right, frame. top + 70, paint); canvas. drawRect (frame. left, frame. bottom-10, frame. left + 70, frame. bottom, paint); canvas. drawRect (frame. left, frame. bottom-70, frame. left + 10, frame. bottom, paint); canvas. drawRect (frame. right-70, fra Me. bottom-10, frame. right, frame. bottom, paint); canvas. drawRect (frame. right-10, frame. bottom-70, frame. right, frame. bottom, paint); // Draw the exterior (I. e. outside the framing rect) darkened paint. setColor (resultBitmap! = Null? ResultColor: maskColor); canvas. drawRect (0, 0, width, frame. top, paint); canvas. drawRect (0, frame. top, frame. left, frame. bottom + 1, paint); canvas. drawRect (frame. right + 1, frame. top, width, frame. bottom + 1, paint); canvas. drawRect (0, frame. bottom + 1, width, height, paint); if (resultBitmap! = Null) {// Draw the opaque result bitmap over the scanning rectangle paint. setAlpha (CURRENT_POINT_OPACITY); canvas. drawBitmap (resultBitmap, null, frame, paint);} else {// paint. setAlpha (SCANNER_ALPHA [scannerAlpha]); // scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length; int middle = frame. height ()/2 + frame. top; laserLinePosition = laserLinePosition + 5; if (laserLinePosition> frame. height () {laserLinePosition = 0;} linearGradient = new LinearGradient (frame. left + 1, frame. top + laserLinePosition, frame. right-1, frame. top + 10 + laserLinePosition, colors, position, Shader. tileMode. CLAMP); // Draw a red "laser finished" line through the middle to show decoding is active // paint. setColor (laserColor); paint. setShader (linearGradient); // draw a scanned canvas. drawRect (frame. left + 1, frame. top + laserLinePosition, frame. right-1, frame. top + 10 + laserLinePosition, paint); paint. setShader (null); float scaleX = frame. width ()/(float) previewFrame. width (); float scaleY = frame. height ()/(float) previewFrame. height (); List
 
  
CurrentPossible = possibleResultPoints; List
  
   
CurrentLast = lastPossibleResultPoints; int frameLeft = frame. left; int frameTop = frame. top; if (currentPossible. isEmpty () {lastPossibleResultPoints = null;} else {possibleResultPoints = new ArrayList <> (5); lastPossibleResultPoints = currentPossible; paint. setAlpha (CURRENT_POINT_OPACITY); paint. setColor (resultPointColor); for (ResultPoint point: currentPossible) {canvas. drawCircle (frameLeft + (Int) (point. getX () * scaleX), frameTop + (int) (point. getY () * scaleY), POINT_SIZE, paint) ;}} if (currentLast! = Null) {paint. setAlpha (CURRENT_POINT_OPACITY/2); paint. setColor (resultPointColor); float radius = POINT_SIZE/2.0f; for (ResultPoint point: currentLast) {canvas. drawCircle (frameLeft + (int) (point. getX () * scaleX), frameTop + (int) (point. getY () * scaleY), radius, paint) ;}} postInvalidateDelayed (16, frame. left, frame. top, frame. right, frame. bottom); // postInvalidate ();}}}
  
 
Activity_main.xml

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.