android Studio 二維碼掃一掃 使用精簡過的zxing

來源:互聯網
上載者:User

標籤:版本   appcompat   port   lease   contex   ring   circle   putextra   textview   

今天學習做個掃一掃  於是就上百度找找前人的作品,終於找到了,於是就開始搞

 

我使用的是 最新的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

想做的APP樣子是:  A介面中有個按鈕。點擊之後跳到掃一掃B介面,掃完了之後,回調到A介面,在A介面彈一個toast

 

首先 引用:

在build.gradle中的dependencies  添加一句話  implementation ‘cn.yipianfengye.android:zxing-library:2.2‘

如果其他版本的Android Studio  引入報錯  就使用 compile引用 。

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中  一共有 三個 layout  兩個 class 一張圖  我命名為 scan  定義了一個顏色資源

 

 1、開啟 colors.xml   添加資源

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

2、複製圖片 scan.png到drawable

3、動手搞A介面

名字activity_camera.xml   布局為:

<?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="掃描二維碼" /></LinearLayout>

 


 QR class代碼:

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(int requestCode, int resultCode, Intent data) {        if(requestCode ==1){            if(data!=null){                Bundle bundle = data.getExtras();                if(bundle==null){                    return ;                                    }                if(bundle.getInt(CodeUtils.RESULT_TYPE)==CodeUtils.RESULT_SUCCESS){                    String ret = bundle.getString(CodeUtils.RESULT_STRING);                    Toast.makeText(this, "掃碼成功,結果:"+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、開始搞B介面

 名字 activity_camera.xml 布局代碼:

<?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"    android: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:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="返回" />    <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="開啟閃光燈" />    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android: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="掃一掃"        android:textColor="@color/encode_view"        android:textSize="24sp" /></RelativeLayout>

 再弄一個布局  用於替換原始掃一掃的那正方形視窗,:

 

 

 名字  camera.xml   布局為:

<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" />    <com.uuzuche.lib_zxing.view.ViewfinderView        android:id="@+id/viewfinder_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:inner_corner_color="@color/scan_corner_color"  //這個是掃一掃四個角的顏色        app:inner_corner_length="30dp"        app:inner_corner_width="2dp"  //這個是掃一掃 四個角的粗細        app:inner_height="200dp"        app:inner_margintop="150dp"        app:inner_scan_bitmap="@drawable/scan"  //這個是掃一掃那條線        app:inner_scan_iscircle="false"        app:inner_scan_speed="50"  //這個是那條線 從上往下的速度        app:inner_width="200dp" /></FrameLayout>

  //這個布局中的id不能更改 否則報null 指標

 // com.uuzuche.lib_zxing.view.ViewfinderView標籤可以根據需要修改  

 

 

Camera  class 代碼為:

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 savedInstanceState) {        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);  //設定自訂掃碼介面        captureFragment.setAnalyzeCallback(analyzeCallback);        Log.d("bug", "onCreate: "+3);        //R.id.fl_zxing_container  對應   setContentView 布局中的  Fragment        getSupportFragmentManager().beginTransaction().replace(R.id.myCamera, captureFragment).commit();  // 替換setContenView設定的布局中的  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();        }    };}

  最後添加許可權

   <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" />

聲明介面

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

  

 

android Studio 二維碼掃一掃 使用精簡過的zxing

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.