Android Studio QR code sweep using streamlined zxing

Source: Internet
Author: User

Today to learn to do a sweep and then on Baidu to find the works of predecessors, and finally found, so began to engage in

I'm using the latest Android Studio

Android Studio 3.1.2
Build #AI -173.4720617, built on April 14, 2018
JRE:1.8.0_152-RELEASE-1024-B02 AMD64
JVM:OPENJDK 64-bit Server VM by JetBrains S.R.O
Windows 10 10.0

Want to do the app looks like: A interface has a button. Click to skip to Sweep B interface, after sweep, callback to a interface, a toast in the A interface

First quote:

Dependencies in Build.gradle add a sentence implementation ' cn.yipianfengye.android:zxing-library:2.2 '

If other versions of Android Studio introduce an error, use the compile reference.

dependencies {    Implementation filetree (dir: ' Libs ', include: [' *.jar '])    implementation ' Com.android.support: appcompat-v7:27.1.1 '    implementation ' com.android.support.constraint:constraint-layout:1.1.0 '    Testimplementation ' junit:junit:4.12 '    androidtestimplementation ' com.android.support.test:runner:1.0.2 '    Androidtestimplementation ' com.android.support.test.espresso:espresso-core:3.0.2 '    implementation ' cn.yipianfengye.android:zxing-library:2.2 '}

Demo in a total of three layout two Class A picture I named scan defines a color resource

1. Open Colors.xml Add resources

<item name= "Scan_color" > #ffffff </item>

2, copy pictures scan.png to drawable

3, hands-on a interface

The name Activity_camera.xml layout is:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:app=" Http://schemas.android.com/apk/res-auto "    xmlns:tools=" http://schemas.android.com/ Tools "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    tools:context=". QR ">    <button        android:id=" @+id/bt_qr "        android:layout_width=" wrap_content "        android:layout_ height= "Wrap_content"        android:layout_weight= "1"        android:text= "scan qr Code"/></linearlayout>

 

QR Class Code:

Package Com.example.administrator.qr;import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.toast;import Com.uuzuche.lib_zxing.activity.codeutils;import Java.nio.charset.coderresult;public class QR extends Appcompatactivity {@Override protected void Onactivityresult (i NT Requestcode, int resultcode, Intent data) {if (Requestcode ==1) {if (data!=null) {Bundl                E bundle = Data.getextras ();                                    if (bundle==null) {return; } if (Bundle.getint (codeutils.result_type) ==codeutils.result_success) {String ret = Bundl                    E.getstring (codeutils.result_string);                Toast.maketext (This, "Sweep code successful, result:" +ret, Toast.length_short). Show ();    }}} super.onactivityresult (Requestcode, ResultCode, data); }    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_QR);        Button button = Findviewbyid (R.ID.BT_QR);                Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                Intent Intent = new Intent (qr.this,camera.class);            Startactivityforresult (intent,1);    }        }); }    }

4. Start to engage B interface

Name Activity_camera.xml Layout code:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "Match_parent" tools:context= ". Camera "> <framelayout android:id=" @+id/mycamera "android:layout_width=" Match_parent "Android : layout_height= "Match_parent" > </FrameLayout> <button android:id= "@+id/bt_back" Android:la        Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Back"/> <Button        Android:id= "@+id/bt_kaideng" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentleft= "false" android:layout_alignparentright= "true" android:layout_alignparenttop        = "true" android:text= "Open flash"/> <textviewAndroid:id= "@+id/textview" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a Ndroid:layout_alignbottom= "@+id/bt_back" android:layout_alignparenttop= "true" android:layout_aligntop= "@+id/        Bt_back "android:layout_centerhorizontal=" true "android:gravity=" fill_vertical "android:text=" Sweep " Android:textcolor= "@color/encode_view" android:textsize= "24SP"/></relativelayout>

And then get a layout to replace that square window with the original sweep,:

The name Camera.xml layout is:

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com        /apk/res-auto "android:layout_width=" fill_parent "android:layout_height=" fill_parent "> <SurfaceView Android:id= "@+id/preview_view" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/&gt    ; <com.uuzuche.lib_zxing.view.viewfinderview android:id= "@+id/viewfinder_view" android:layout_width= "Wrap_c Ontent "android:layout_height=" wrap_content "app:inner_corner_color=" @color/scan_corner_color "//This is a sweep sweep four corners of Color app:inner_corner_length= "30DP" app:inner_corner_width= "2DP"//This is a sweep sweep four corners of the thickness app:inner_height= "200 DP "app:inner_margintop=" 150DP "app:inner_scan_bitmap=" @drawable/scan "//This is the sweep of the line app:inner_scan_is  Circle= "false" app:inner_scan_speed= "50"//This is the line from the top down to the speed app:inner_width= "200DP"/></framelayout>

The ID in this layout cannot be changed or the null pointer is reported

Com.uuzuche.lib_zxing.view.ViewfinderView tags can be modified as needed

The Camera Class code is:

Package Com.example.administrator.qr;import Android.content.intent;import Android.graphics.bitmap;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.util.log;import Android.view.windowmanager;import Com.uuzuche.lib_zxing.activity.capturefragment;import Com.uuzuche.lib_ Zxing.activity.codeutils;public class Camera extends Appcompatactivity {@Override protected void onCreate (Bundle sa        Vedinstancestate) {super.oncreate (savedinstancestate);        Getsupportactionbar (). Hide ();        GetWindow (). SetFlags (WindowManager.LayoutParams.ALPHA_CHANGED, WindowManager.LayoutParams.ALPHA_CHANGED);        Setcontentview (R.layout.activity_camera);//LOG.D ("Bug", "onCreate:" + 1);        Capturefragment capturefragment = new Capturefragment ();        LOG.D ("Bug", "OnCreate:" +2);  Codeutils.setfragmentargs (Capturefragment,r.layout.camera);        Set Custom Scan interface Capturefragment.setanalyzecallback (analyzecallback); LOG.D ("Bug", "OnCreate: "+3); The r.id.fl_zxing_container corresponds to the Fragment Getsupportfragmentmanager () in the Setcontentview layout. BeginTransaction (). Replac  E (R.id.mycamera, capturefragment). commit ();  Replace Setcontenview settings in the layout with ID Mycamera} codeutils.analyzecallback analyzecallback = new Codeutils.analyzecallback () {@Override public void onanalyzesuccess (Bitmap mbitmap, String result) {Intent resultintent =            New Intent ();            Bundle bundle = new bundle ();            Bundle.putint (Codeutils.result_type, codeutils.result_success);            Bundle.putstring (codeutils.result_string, RESULT);            Resultintent.putextras (bundle);            Camera.this.setResult (RESULT_OK, resultintent);        Camera.this.finish ();            } @Override public void onanalyzefailed () {Intent resultintent = new Intent ();            Bundle bundle = new bundle ();            Bundle.putint (Codeutils.result_type, codeutils.result_failed); Bundle.putString (Codeutils.result_string, "");            Resultintent.putextras (bundle);            Camera.this.setResult (RESULT_OK, resultintent);        Camera.this.finish (); }    };}

Last Add permission

   <uses-permission android:name= "Android.permission.CAMERA"/>    <uses-permission android:name= " Android.permission.FLASHLIGHT "/>    <uses-feature android:name=" Android.hardware.camera "/>    < Uses-feature android:name= "Android.hardware.camera.autofocus"/>    <uses-permission android:name= " Android.permission.VIBRATE "/>    <uses-permission android:name=" Android.permission.WAKE_LOCK "/>    <uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>    <uses-permission Android : Name= "Android.permission.INTERNET"/>

Declaration interface

<activity android:name= "Com.example.administrator.qr.Camera" >

  

Android Studio QR code sweep using streamlined zxing

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.