如何在Google Vision条形码阅读器API中使用手电筒/手电筒?

时间:2016-06-19 15:07:23

标签: android google-play-services barcode-scanner flashlight google-vision

我对this questionthis question提出了类似的问题,但两者都没有可接受的解决方案。

我基本上使用Google Vision条形码API,但似乎没有明显的方法可以控制手电筒。

This answer建议使用this code,但(尝试过但失败了)我不确定如何将其集成到我的应用中。

以下是我的活动代码,它基本上启动了相机/条形码扫描仪,还使用了我想要用来切换手电筒的BaseActivity中的菜单项:

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;

public class ScanQRCodeAutoActivity extends BaseActivity {

    private static final String LOG_TAG = ScanQRCodeAutoActivity.class.getSimpleName();

    private static final int CAMERA_SOURCE_PREVIEW_WIDTH = -1;
    private static final int CAMERA_SOURCE_PREVIEW_HEIGHT = 480;
    public static final String QR_CODE_VALUE = "QR_CODE_VALUE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_qr_code_auto);

        super.initFlash(savedInstanceState);

        final SurfaceView cameraSurfaceView = (SurfaceView)findViewById(R.id.cameraSurfaceView);

        BarcodeDetector.Builder barcodeDetectorBuilder = new BarcodeDetector.Builder(this);
        barcodeDetectorBuilder.setBarcodeFormats(Barcode.QR_CODE);
        BarcodeDetector barcodeDetector = barcodeDetectorBuilder.build();
        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {

                final SparseArray<Barcode> barcodes = detections.getDetectedItems();

                if (barcodes.size() != 0) {

                    Intent data = new Intent();
                    data.putExtra(QR_CODE_VALUE, barcodes.valueAt(0).displayValue);
                    setResult(CommonStatusCodes.SUCCESS, data);
                    finish();

                }

            }
        });

        CameraSource.Builder cameraSourceBuilder = new CameraSource.Builder(this, barcodeDetector);
        //cameraSourceBuilder.setAutoFocusEnabled(true);
        //cameraSourceBuilder.setFacing(CameraSource.CAMERA_FACING_BACK);
        if (CAMERA_SOURCE_PREVIEW_WIDTH != -1 && CAMERA_SOURCE_PREVIEW_HEIGHT != -1) {
            cameraSourceBuilder.setRequestedPreviewSize(CAMERA_SOURCE_PREVIEW_WIDTH, CAMERA_SOURCE_PREVIEW_HEIGHT);
        }

        final CameraSource cameraSource = cameraSourceBuilder.build();

        cameraSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                try {
                    cameraSource.start(cameraSurfaceView.getHolder());

                }
                catch (SecurityException se) {
                    Log.e(LOG_TAG, "Could not start camera source.", se);
                }
                catch (IOException ioe) {
                    Log.e(LOG_TAG, "Couldn't start camera source.", ioe);
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

                cameraSource.stop();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        MenuItem flashItem = menu.findItem(R.id.action_flash);
        flashItem.setVisible(true);

        return true;
    }

    @Override
    protected void toggleFlash() {

        super.toggleFlash();

        // HELP!!

        invalidateOptionsMenu();
        if (isFlashOn()) {
            Utilities.makeToast(this, "Flash turned on");
        }
        else {
            Utilities.makeToast(this, "Flash turned off");
        }

    }
}

1 个答案:

答案 0 :(得分:-2)

请参阅以下链接,其中我已发布此问题的答案,

https://stackoverflow.com/a/40908844/6409743