0
votes

I'm accessing the camera using the SurfaceView and CameraSource and showing the image on the screen. The application also need to use the flash light. The camera view freezes when I turn on the flash light. I don't want this to happen. I don't understand why. When the flash light is on, the camera must be active at the same time. For example, taking pictures with the flash light on the phone. I am using the following codes. I'd appreciate it if you could help or indicate what caused the problem.

*My code is working. But when I turn on the flashlight, the camera image remains attached.

MyActivity

public class MyActivity extends AppCompatActivity {

    SurfaceView cameraPreview;
    CameraSource cameraSource;
    final int RequestCameraPermissionID = 1001;

    ImageButton Flash_On_Button;

    private Camera cam;
    Camera.Parameters p;

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RequestCameraPermissionID: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    try {
                        cameraSource.start(cameraPreview.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
    }

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

        Flash_On_Button  = findViewById(R.id.Flash_On_Button);
        Flash_On_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FlashON();          
            }
        });

        cameraPreview  = findViewById(R.id.cameraPreview);
        cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .setAutoFocusEnabled(true)
                .build();

        CameraStart() ;
}


    private void CameraStart() {

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

                            ActivityCompat.requestPermissions(ScanActivity.this,
                                    new String[]{Manifest.permission.CAMERA},RequestCameraPermissionID);
                            return;
                        }
                        try {
                            cameraSource.start(cameraPreview.getHolder());
                            Toast.makeText(ScanActivity.this, "surface Created", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                        Toast.makeText(ScanActivity.this, "surface Changed", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                        cameraSource.stop();
                        Toast.makeText(ScanActivity.this, "surface Destroyed", Toast.LENGTH_SHORT).show();
                    }
                });

    }


    public void FlashON(){

            try {
                if (getPackageManager().hasSystemFeature(
                        PackageManager.FEATURE_CAMERA_FLASH)) {

                    cam = Camera.open();
                    p = cam.getParameters();

                    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    cam.setParameters(p);
                    cam.startPreview();

                }
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "Exception flashLightOn()", Toast.LENGTH_SHORT).show();
            }

        }
}
1
I don't think you can mix the deprecated Camera API with CameraSource. I can't even understand how Camera.open() executes, in your code, without throwing -- CameraSource has already opened the camera! And since it's already previewing, it's almost certainly the startPreview() call that freezes the screen. Try removing that. Otherwise, I don't know how to get a Camera from a CameraSource, so I can't be of much help. Maybe try this.greeble31
I removed the startpreview (). But the problem continues. I need to use the Camera Source with SurfaceView to process the image on the screen. "Camerasource.start (camerapreview.gethold is ())" I need to turn on the flash light while the screen is alive. I need to access the camera as a hardware and start the camera to open the flash. The image freezes and flashes when I start it. "Cam.start ()" Thanks for your reply. @greeble31user10260682
I mean, obviously you can do this; others have done it. What I'm trying to say is, you can't do it using a CameraSource. At least I really don't think so. You're going to need to re-architect your app to use Detector.detect() instead. You're going to have to set up your own frame pipeline. You'll probably have to find a tutorial somewhere to help.greeble31

1 Answers

1
votes

You can try implementing the following codes:

Availability of flash light

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

Enable On/Off of the flash light, include this in your Android Manifest

 <!-- Allows access to the flashlight -->
 <permission android:name="android.permission.FLASHLIGHT"
         android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
         android:protectionLevel="normal"
         android:label="@string/permlab_flashlight"
         android:description="@string/permdesc_flashlight" /> 

To turn on camera flashlight

Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

To turn off camera flashlight

cam.stopPreview();
cam.release();

The complete discussion can be found here