android Studio 二维码扫一扫 使用精简过的zxing

摘要:
今天,我学会了扫一扫,于是我去百度寻找前人的作品。最后,我找到了它们,所以我开始使用最新的androidStudioAndroidStudio3.1.2Build#AI-173.4720617,buildonAprl142018JRE:1.8.0_152-release-1024-b02amd64JVM:OpenJDK64-BitServerVMbyJetBrains.r.oWindows10

今天学习做个扫一扫  于是就上百度找找前人的作品,终于找到了,于是就开始搞

我使用的是 最新的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  定义了一个颜色资源

android Studio 二维码扫一扫 使用精简过的zxing第1张

 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: 
        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: 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>
    <Button
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />

    <Button
        android: 
        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: 
        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>

 再弄一个布局  用于替换原始扫一扫的那正方形窗口,如图所示:

 android Studio 二维码扫一扫 使用精简过的zxing第2张

 名字  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: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.uuzuche.lib_zxing.view.ViewfinderView
        android: 
        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不能更改 否则报空指针

 // 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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Spring中使用RedisTemplate操作Redis(spring-data-redis)windows ------ 证书的导出下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

安卓手机屏幕投射到电脑以及一台电脑控制多台手机技术原理浅析

奥创软件研究院推出的奥创群控让越来越多的人了解到了电脑控制手机的操作。自奥创软件研究院首家发布电脑批量控制手机的解决方案以来,有很多人开始探讨电脑控制手机技术在实际工作中的应用。 由于市场太大,仅靠奥创软件研究院一家也是做不过来的,现在将手机屏幕投射到到电脑的技术原理,以及一台电脑批量控制多台手机的技术(即所谓的手机反响控制)简单的给大家介绍下,在此抛砖引...

Android-TextView 控件常用属性以及基本用法

github地址:https://github.com/1165863642/TextViewDemo 前言 这是我第一次写博客,第一次的笔记,不足之处多谅解。开门见山,这一篇博客主要讲一下在Android开发中,UI控件TextView的一些使用方式,主要介绍一些常用的属性,加上一些实例,希望大家看了后能够掌握其基本用法。 【基本属性】 【设置ID】 a...

Equinox OSGi系列之 创建自己的OSGi应用项目

1、摘要 前述文档我向大家展示了Equinox OSGi环境及其搭建配置。从本文开始,我们将详细讨论Bundle的开发及OSGi应用构建。 2、OSGi相关概念 在正式进入Bundle的设计与开发之前,我们先来熟悉一下OSGi框架中的一些概念。用户在设计Bundle时必须要深入理解这些实体概念。 实体概念 实体概念说明 Bundle - 安装到OS...

android界面布局

(一)LinearLayout界面编排模式 他的格式是按照线性顺序,由上往下或右左往右,逐一排列界面组件。 layout_中的“match_parent”表示要填满他所在的外框,而“wrap_content”表示它的大小只要满足内部所包含的界面组件即可。 android:orientation:“horizontal”表示排列方式为水平,而“vertica...

安卓界面组件单选复选按框

    安卓界面UI为开发者提供了单选复选框,单选框和复选框继承了安卓的普通按钮,所以他们可以直接使用普通按钮的xml基本属性。他们与普通按钮区别在于提供了可以选中的功能,同时二者之间的区别是:单选按钮在按钮组中,当用户点击选择以后,这个按钮组就必须有一个被选中,只能改选,无法撤销(所以单独的一个单选框功能上很尴尬,点击以后状态不能再更改,很僵硬);复选框...

Android2.3.7源码结构分析

对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分。为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析。希望对刚加入安卓大军的朋友们有所帮助。下图为2.3.7_r1版本的根目录结构:Makefile (makefile文件) bionic (bionic C库,...