打开相机并扫描QR码的安卓API?

6
我正在尝试开发一个应用程序,可以打开相机并扫描给定的QR码。然而,我有一个限制条件,即我不能使用任何第三方库,比如ZBar Code Reader或ZXingScanner,因为这会增加我的apk文件的大小。
我在研究谷歌的条形码APIhere,但它似乎不能执行我想要的操作,也就是不会打开任何相机并对图像进行实时扫描。
那么,我该如何使我的应用程序在特定范围内打开相机并对QR码进行实时扫描呢?
感谢您的时间!

我不相信这是可能的。毕竟,你必须使用零字节的代码来完成这个任务,否则它会增加你的 APK 的大小。 - CommonsWare
3
@Auro:条形码 API 文档不仅限于你分享的链接(例如高级示例)... 当然,您也可以通过 Google API 在相机中实时读取条形码。 请查看 https://www.varvet.com/blog/android-qr-code-reader-made-easy/,了解使用 Google API 进行条形码(QR 码)扫描的简化版本演示。 - Algar
1个回答

0

Google条形码API提供打开相机的支持。以下是示例代码

    // A barcode detector is created to track barcodes.  An associated multi-processor instance
    // is set to receive the barcode detection results, track the barcodes, and maintain
    // graphics for each barcode on screen.  The factory is used by the multi-processor to
    // create a separate tracker instance for each barcode.
    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
    barcodeDetector.setProcessor(
            new MultiProcessor.Builder<>(barcodeFactory).build());

    if (!barcodeDetector.isOperational()) {
        // Note: The first time that an app using the barcode or face API is installed on a
        // device, GMS will download a native libraries to the device in order to do detection.
        // Usually this completes before the app is run for the first time.  But if that
        // download has not yet completed, then the above call will not detect any barcodes
        // and/or faces.
        //
        // isOperational() can be used to check if the required native libraries are currently
        // available.  The detectors will automatically become operational once the library
        // downloads complete on device.
        Log.w(TAG, "Detector dependencies are not yet available.");

        // Check for low storage.  If there is low storage, the native library will not be
        // downloaded, so detection will not become operational.
        IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
        boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;

        if (hasLowStorage) {
            Toast.makeText(this, R.string.low_storage_error, Toast.LENGTH_LONG).show();
            Log.w(TAG, getString(R.string.low_storage_error));
        }
    }

    // Creates and starts the camera.  Note that this uses a higher resolution in comparison
    // to other detection examples to enable the barcode detector to detect small barcodes
    // at long distances.
    CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedPreviewSize(1600, 1024)
            .setRequestedFps(15.0f);

    // make sure that auto focus is an available option
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        builder = builder.setFocusMode(
                autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
    }

    mCameraSource = builder
            .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
            .build();
}

你可以参考谷歌的条形码示例

https://github.com/googlesamples/android-vision/tree/master/visionSamples/barcode-reader


有没有关于他们如何创建CameraSource的逐步教程,或者仅仅是在检测到QR码时立即进行扫描(而不触发捕获)的教程? - Tenten Ponce

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接