Skip to content

Make you have the QRCode Scan ability like WeChat!

Notifications You must be signed in to change notification settings

devilsen/CZXing

Repository files navigation

GitHub release Bintray

CZXing

C++ port of ZXing for Android

底层使用C++来处理图像及解析二维码,并且加入了OpenCV来解析图像,可以在更远的距离识别出二维码。

使用

在gradle中:

implementation 'me.devilsen:CZXing:0.9.7'

建议加入abiFilters

    defaultConfig {
        
        // 其他设置...

        ndk {
            // 设置支持的so库架构,设置一个可以减小包的大小
            abiFilters "armeabi-v7a","arm64-v8a"
        }
    }

1. 直接使用

你可以直接使用已经封装好的ScanActivity作为扫码界面

Resources resources = getResources();
List<Integer> scanColors = Arrays.asList(resources.getColor(R.color.scan_side), resources.getColor(R.color.scan_partial), resources.getColor(R.color.scan_middle));

Scanner.with(this)
        .setBorderColor(resources.getColor(R.color.box_line))   // 扫码框边框颜色
        .setCornerColor(resources.getColor(R.color.corner))     // 扫码框角颜色
        .setScanLineColors(scanColors)                          // 扫描线颜色(这是一个渐变颜色)
        .setScanMode(ScanView.SCAN_MODE_TINY)                   // 扫描区域
        .setTitle("My Scan View")                               // 扫码界面标题
        .showAlbum(true)                                        // 显示相册(默认为true)
        .setBarcodeFormat(BarcodeFormat.EAN_13)                 // 设置扫码格式
        .setScanNoticeText("扫描二维码")                         // 设置扫码文字提示
        .setFlashLightOnText("打开闪光灯")                       // 打开闪光灯提示
        .setFlashLightOffText("关闭闪光灯")                      // 关闭闪光灯提示
        .continuousScan()                                       // 连续扫码,不关闭扫码界面
        .setOnClickAlbumDelegate(new ScanActivityDelegate.OnClickAlbumDelegate() {
            @Override
            public void onClickAlbum(Activity activity) {       // 点击右上角的相册按钮
                Intent albumIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                activity.startActivityForResult(albumIntent, CODE_SELECT_IMAGE);
            }

            @Override
            public void onSelectData(int requestCode, Intent data) { // 选择图片返回的数据
                if (requestCode == CODE_SELECT_IMAGE) {
                    selectPic(data);
                }
            }
        })
        .setOnScanResultDelegate(new ScanActivityDelegate.OnScanDelegate() { // 接管扫码成功的数据
            @Override
            public void onScanResult(String result, BarcodeFormat format) {
                Intent intent = new Intent(MainActivity.this, DelegateActivity.class);
                intent.putExtra("result", result);
                startActivity(intent);
            }
        })
        .start();

2. 自定义界面

或者使用ScanView来自定义你的界面

<me.devilsen.czxing.view.ScanView
    android:id="@+id/surface_view_scan"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在自定义的Activity中你需要接管ScanView的生命周期,具体可以参看ScanActivity界面,同时设定setScanListener()

mScanView.setScanListener(new ScanListener() {
    @Override
    public void onScanSuccess(String result, BarcodeFormat format) {
        // 扫码成功
    }

    @Override
    public void onOpenCameraError() {
        // 打开相机出错
    }
});

3. 生成二维码

调用以下代码,可生成二维码的bitmap,Color为可选参数,默认为黑色。

BarcodeWriter writer = new BarcodeWriter();
Bitmap bitmap = writer.write("Hello World", BarCodeUtil.dp2px(this, 200), BarCodeUtil.dp2px(this, 200), Color.RED);

效果展示

点击观看

apk下载

设计思路

License

Copyright (C) 2012 The Android Open Source Project
Copyright 2019 Devilsen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.