Andoid studio 使用Zxing扫描二维码

原文:http://blog.csdn.net/qq_30379689/article/details/52411489

新手刚开搞android一周,各种折腾了好久终于搞定了这个二维码扫描,太蛋疼了。

作为Google开源框架Zxing,里面的文件很大,这里主要讲的是精简ZXing项目后只保留扫描功能的代码,可以缩小项目的大小,对于只要扫描功能的项目已经够用了。扫描后的结果,只要通过WebView百度一下就出来了。简单的说,可以把Zxing这个二维码扫描功能当做一个第三方服务来使用,本篇文章分为两部分,Zxing的集成和Zxing的使用

 

一:下载我们所需要的Zxing精简版,在Github上搜索Zxing,看到这条记录,然后下载。

百度网盘下载:链接:http://pan.baidu.com/s/1c12ybh2 密码:bpix

 

 

二:复制到项目中,在ZXingProj/src/com/dtr目录下,复制这个zxing文件夹到我们的项目中

 编译一下就会发现一堆错误,下面我们就去改错误。

 

 三、复制zxing.jar包到libs

 

 

四、复制下载的res的layout文件、res的values的ids文件、raw文件夹、res的drawable-xhdpi文件夹到我们项目的对应位置

 

 

 

 

五、重新导入文件,记得删除原先的R包,换成自己项目的R包 

修改:

 

 

全部修改完重新编译无错误

 

六.声明权限和Activity

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sdtimoniyor.ticketsystem">
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Home">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Moitor.MonitorHome"></activity>
        <activity
            android:name=".zxing.activity.CaptureActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" />
        <activity
            android:name=".zxing.activity.ResultActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" />
    </application>

</manifest>

 

七、启动

 Button qrcode=(Button)findViewById(R.id.qr_code);
        qrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MonitorHome.this, CaptureActivity.class);
                startActivity(intent);
            }
        });

 

八、Test

 

 

 我擦,这是什么鬼,回去一看权限也加了啊

 

九、修改,添加运行时权限

 

 Button qrcode = (Button) findViewById(R.id.qr_code);
        qrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MonitorHome.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MonitorHome.this, new String[]{Manifest.permission.CAMERA}, 1);
                } else {
                    Intent intent = new Intent(MonitorHome.this, CaptureActivity.class);
                    startActivity(intent);
                }
            }
        });

 

 

 

 十、真机测试

 

 

结尾:这图真尼玛大。

 

posted @ 2017-08-10 10:13  XinYiBuFang  阅读(17146)  评论(1编辑  收藏  举报