Turn on Flashlight with google vision?

0

I'm doing an application to detect words through the camera with the api google vision and I would like to know how to turn on the flash while in the application.

here I leave the main activity code where the camera instance and the permissions are created ...

I followed the tutorial of link

  

protected void onCreate (Bundle savedInstanceState) {           super.onCreate (savedInstanceState);           setContentView (R.layout.activity_main);

    cameraView = (SurfaceView) findViewById(R.id.surface_view);
    textView = (TextView) findViewById(R.id.text_view);
    textView.setText("\nDetectando...");
    btnDefinicion = (Button) findViewById(R.id.verDefinicion);
    btnSugerencias = (Button) findViewById(R.id.verSugerencias);

    btnDefinicion.setEnabled(false);
    btnSugerencias.setEnabled(false);

    //flash-----------------------------

    //flash---------------------------


    TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
    if (!textRecognizer.isOperational()) {
        Log.w("MainActivity", "No dependencias listas");
    } else {
        cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1900, 1080)
                //.setRequestedPreviewSize(240, 160)
                .setRequestedFps(2.0f)
                .setAutoFocusEnabled(true)
                .build();



        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.CAMERA},
                                RequestCameraPermissionID);
                        return;
                    }
                    cameraSource.start(cameraView.getHolder());

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();
            }
        });

        textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<TextBlock> detections) {
                final SparseArray<TextBlock> items = detections.getDetectedItems();
                if(items.size() !=0){
                    textView.post(new Runnable() {
                        @Override
                        public void run() {
                            StringBuilder stringBuilder = new StringBuilder();

                        TextBlock item = items.valueAt(0);
                        stringBuilder.append(item.getValue());
                        //stringBuilder.append("\n");

                        String bloquesPalabras = stringBuilder.toString();

                        procesarBloques(bloquesPalabras);

                        //textView.setText("Se encontró:"+"\n"+devolverPalabra(stringpalabra));
                        }
                    });
                }
            }
        });

    }


}
    
asked by Zaifer 13.08.2018 в 00:38
source

1 answer

0

Here I leave you two methods to turn the light on and off

first the permissions on the manifest

<uses-permission android:name="android.permission.CAMERA" />

Then to turn on and off the camera light

public static Camera cam = null; //tiene que ser estatico para que onDestroy no lo destruya



 public void prenderLuz() {

        try {
            //Chequeamos si el dispositivo cuenta con flash para poder activarlo
            if (getPackageManager().hasSystemFeature(
                    PackageManager.FEATURE_CAMERA_FLASH)) {
                cam = Camera.open();
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getBaseContext(), "Problema al encender el flash de su dispositivo",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void apagarLuz() {
        try {
            if (getPackageManager().hasSystemFeature(
                    PackageManager.FEATURE_CAMERA_FLASH)) {
                cam.stopPreview();
                cam.release();
                cam = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
           Toast.makeText(getBaseContext(), "Problema al apagar el flash de su dispositivo.",
                    Toast.LENGTH_SHORT).show();
        }
        }
    }

where it says //flash----- in your code just put the method to turn on the light

prenderLuz();
    
answered by 13.08.2018 в 03:42