Enable flash camera

asked 2020-03-19 06:18:21 -0600

Raf gravatar image

updated 2020-03-19 06:35:23 -0600

berak gravatar image

I am currently using JavaCameraView to enable flash. Unfortunately the APIs of camera1 have been deprecated in API 21 and therefore I am forced to switch to JavaCamaera2View. The problem is that I can't enable the flash because I don't have access to the camera like in JavaCameraView. My version OpenCV is 3.4.6 Do you have any solutions?

This is my code now:

public class CustomCamera extends JavaCameraView implements PictureCallback { 

    private String mPictureFileName;

    public CustomCamera(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public List<String> getEffectList() {
        return mCamera.getParameters().getSupportedFlashModes();
    }
    public boolean isEffectSupported() {
        return (mCamera.getParameters().getFlashMode() != null);

    }
    public String getEffect() {
        return mCamera.getParameters().getFlashMode();
    }
    public void setEffect(String effect) {
        mCamera.getParameters();
        Camera.Parameters params = mCamera.getParameters();
    params.setFlashMode(effect);
    mCamera.setParameters(params);
}
public List<Size> getResolutionList() {
    return mCamera.getParameters().getSupportedPreviewSizes();
}
public void setResolution(int w, int h) {
    disconnectCamera();
    mMaxHeight = h;
    mMaxWidth = w;
    connectCamera(getWidth(), getHeight());
}

public Size getResolution() {
    return mCamera.getParameters().getPreviewSize();

}

public void takePicture(final String fileName) {
    this.mPictureFileName = fileName;
    // Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
    // Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
mCamera.setPreviewCallback(null);

// PictureCallback is implemented by the current class
mCamera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)


  try {
        FileOutputStream fos = new FileOutputStream(mPictureFileName);
        fos.write(data);
        fos.close();
    } catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
    }

}

public void cameraRelease() {
    if(mCamera != null){
        mCamera.release();
    }
}
edit retag flag offensive close merge delete

Comments

and how would your code / be related to opencv ? please explain.

berak gravatar imageberak ( 2020-03-19 06:36:24 -0600 )edit

I would like to use JavaCamera2 but it does not allow me access to the camera as JavaCameraView. Is there a solution?

Raf gravatar imageRaf ( 2020-03-19 06:38:54 -0600 )edit

That still sounds like an Android programming question more suited toward a site like StackOverflow. berak is asking what makes your question specific to OpenCV.

ssokolow gravatar imagessokolow ( 2020-03-23 17:38:25 -0600 )edit