java安卓开发——2.集成zxing扫描

  1. 下载源码 此为java版本 https://github.com/zxing/zxing
    c#版本地址为 https://github.com/Redth/ZXing.Net.Mobile

  2. 新建安卓项目(接上文 1.新项目搭建)
    将源码 android\src\com\google文件夹 复制到项目目录中
    在这里插入图片描述
    在这里插入图片描述
    将android-core\src\main\java\com\google\zxing\client\android\camera目录下文件复制到项目
    在这里插入图片描述
    复制android\res文件夹下
    目录xml values raw menu layout drawable drawable-xxhdpi (values下strings.xml复制“values-zh-rCN”,此为简体中文)
    在这里插入图片描述
    更改配置文件AndroidManifest.xml添加权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.client01.apptest001">
    <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"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <!-- unavailable in API 23 -->
    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27"/>
    <uses-feature android:name="android.hardware.camera.any"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.flash" android:required="false"/>
    <uses-feature android:name="android.hardware.screen.landscape"/>
    <uses-feature android:name="android.hardware.wifi" android:required="false"/>
    <application android:icon="@drawable/launcher_icon"
                 android:logo="@drawable/launcher_icon"
                 android:label="@string/app_name"
                 android:allowBackup="true">
        <activity android:name="com.google.zxing.client.android.CaptureActivity"
                  android:screenOrientation="sensorLandscape"
                  android:clearTaskOnLaunch="true"
                  android:stateNotNeeded="true"
                  android:theme="@style/CaptureTheme"
                  android:windowSoftInputMode="stateAlwaysHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <!-- Allow web apps to launch Barcode Scanner by linking to http://zxing.appspot.com/scan. -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http" android:host="zxing.appspot.com" android:path="/scan"/>
            </intent-filter>
            <!-- We also support a Google Product Search URL. -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http" android:host="www.google.com" android:path="/m/products/scan"/>
            </intent-filter>
            <!-- And the UK version. -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http" android:host="www.google.co.uk" android:path="/m/products/scan"/>
            </intent-filter>
            <!-- Support zxing://scan/?... like iPhone app -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="zxing" android:host="scan" android:path="/"/>
            </intent-filter>
        </activity>
        <activity android:name="com.google.zxing.client.android.PreferencesActivity"
                  android:label="@string/preferences_name"
                  android:stateNotNeeded="true"/>
        <activity android:name="com.google.zxing.client.android.encode.EncodeActivity"
                  android:stateNotNeeded="true">
            <intent-filter>
                <action android:name="com.google.zxing.client.android.ENCODE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <!-- This allows us to handle the Share button in Contacts. -->
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/x-vcard"/>
            </intent-filter>
            <!-- This allows us to handle sharing any plain text . -->
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
        <activity android:name="com.google.zxing.client.android.book.SearchBookContentsActivity"
                  android:label="@string/sbc_name"
                  android:stateNotNeeded="true"
                  android:screenOrientation="sensorLandscape">
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SEARCH_BOOK_CONTENTS"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name="com.google.zxing.client.android.share.ShareActivity"
                  android:stateNotNeeded="true"
                  android:screenOrientation="user">
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SHARE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name="com.google.zxing.client.android.history.HistoryActivity"
                  android:label="@string/history_title"
                  android:stateNotNeeded="true"/>
        <activity android:name="com.google.zxing.client.android.share.BookmarkPickerActivity"
                  android:label="@string/bookmark_picker_name"
                  android:stateNotNeeded="true"/>
        <activity android:name="com.google.zxing.client.android.share.AppPickerActivity"
                  android:label="@string/app_picker_name"
                  android:stateNotNeeded="true"/>
        <activity android:name="com.google.zxing.client.android.HelpActivity"
                  android:label="@string/menu_help"
                  android:screenOrientation="user"
                  android:stateNotNeeded="true"/>
    </application>

</manifest>

报错解决
在这里插入图片描述
报错基本为R文件路径问题,引入自己的路径即可,然后rebuild 项目

import com.example.client01.apptest001.R;

运行(我直接连的外界设备,没有就连虚机,报 很遗憾,Android 相机出现问题。你可能需要重启设备 错误时去手机设置里把权限开启)
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值