我对这个问题和这个问题有一个类似的问题,这两个问题都没有公认的解决方案。
我基本上使用的是 Google Vision 条形码 API,但似乎没有明显的方法来控制手电筒。
这个答案建议使用这个代码,但是(尝试过但失败了)我不确定如何将它集成到我的应用程序中。
这是我的活动的代码,它基本上启动了相机/条形码扫描仪,还使用了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");
}
}
}