我想在android studio上为我的代码添加手电筒控件(适用于android 6) . 我正在使用谷歌愿景,这是我的实际代码:

Android清单:

<uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.FLASHLIGHT"/>
        
        <uses-feature android:name="android.hardware.camera" />
        <uses-feature android:name="android.hardware.camera.flash" />

摇篮:

compile 'com.google.android.gms:play-services-vision:11.0.4'

XML文件:

<SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foregroundGravity="center" />

    <TextView
        android:id="@+id/barcode_value"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="No Barcode"
        android:textSize="20sp"
        android:visibility="gone"
        tools:layout_editor_absoluteX="119dp"
        tools:layout_editor_absoluteY="474dp" />

活动:

public class Qr3扩展AppCompatActivity {private BarcodeDetector barcodeDetector;
私人CameraSource cameraSource;
私人SurfaceView cameraView;
private TextView barcodeValue;
private boolean flashavAilable = false;
private boolean hasFlash;
private boolean isFlashOn;

#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_qr3);
//开始验证flashLight
hasFlash = getApplicationContext() . getPackageManager() . hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash){
//警告“noflash存在”
其他{
//提醒“flash存在”
flashavAilable = true;

} //结束验证flashLight

int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if(permission!= PackageManager.PERMISSION_GRANTED){makeRequest();}

cameraView =(SurfaceView)findViewById(R.id.surface_view);
barcodeValue =(TextView)findViewById(R.id.barcode_value);

barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
. Build ();
cameraSource = new CameraSource.Builder(this,barcodeDetector)
.setAutoFocusEnabled(真)
.setRequestedFps(60.0f)
.setFacing(CameraSource.CAMERA_FACING_BACK)
. Build ();
cameraView.getHolder() . addCallback(new SurfaceHolder.Callback(){
@覆盖
public void surfaceCreated(SurfaceHolder holder){
尝试{
cameraSource.start(cameraView.getHolder());
} catch(IOException ex){
ex.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){}
#Override
public void surfaceDestroyed(SurfaceHolder holder){cameraSource.stop();}});
#Override
public void receiveDetections(Detector.Detections <Barcode>检测){
final SparseArray <Barcode> barcodes = detections.getDetectedItems();
if(barcodes.size()!= 0){
barcodeValue.post(new Runnable(){
#Override
public void run(){
//将条形码值更新为TextView
barcodeValue.setText(barcodes.valueAt(0).displayValue);
cameraSource.stop();
AlertDialog.Builder dlgAlert = new
AlertDialog.Builder(Qr3.this);
dlgAlert.setMessage(barcodes.valueAt(0).displayValue);
dlgAlert.setTitle( “ Value :”);
dlgAlert.setPositiveButton(“Again”,newDialogInterface.OnClickListener(){覆盖public void onClick(DialogInterface> dialog,int which){try {// noinspection MissingPermission cameraSource.start(cameraView.getHolder());} catch(IOException ex) {ex.printStackTrace();}}}); dlgAlert.setNegativeButton(“End”,new DialogInterface.OnClickListener(){#Override public void onClick(DialogInterface dialog,int which){finish();}}); dlgAlert.setCancelable(真); dlgAlert.create()表示(); }}; }); } // end onCreate // ascolta click pulsanti del volume per accendere o spegnere flash #Override public boolean dispatchKeyEvent(KeyEvent event){int action = event.getAction(); int keyCode = event.getKeyCode(); switch(keyCode){case KeyEvent.KEYCODE_VOLUME_UP:if(flashavAilable){if(action == KeyEvent.ACTION_DOWN){turnOnFlash();返回true; case KeyEvent.KEYCODE_VOLUME_DOWN:if(flashavAilable){if(action == KeyEvent.ACTION_DOWN){turnOffFlash();返回true; default:// riabilita fullscreen return super.dispatchKeyEvent(event); private void turnOnFlash(){if(!isFlashOn){//启用手电筒<------------------------------ ------- HELP isFlashOn = true; cameraSource.stop(); private void turnOffFlash(){if(isFlashOn){//关闭手电筒<------------------------------- ------ HELP isFlashOn = false; }}}

我该怎么控制flashLight?

(任何人都可以在我的问题中修复代码的格式吗?)

谢谢