I am trying to use a barcode reader by implementing a CameraSource, and by doing so, I no longer am able to autofocus on devices that should be able to do so.
cameraSource = new CameraSource.Builder(this, detector).setAutoFocusEnabled(true).build();
I used this, but then the logcat shouted at me:
I/CameraSource: Camera auto focus is not supported on this device.
So, I dig around on stackoverflow like here, here and here. At the end here is what I have:
FrameLayout cameraFrame = (FrameLayout) findViewById(R.id.welcome_camera);
if (cameraFrame != null) {
cameraFrame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();
for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
Camera camera = (Camera) field.get(cameraSource);
if (camera != null) {
Camera.Parameters params = camera.getParameters();
for (Camera.Size size : params.getSupportedPictureSizes()) {
Log.d("Sizes", size.height + " and width " + size.width);
}
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(params);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
});
cameraFrame.addView(preview);
}
I want to know the sizes, so that for the xml I can change the sizes. The size is also confirmed from the surfaceChanged
function here:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
cameraSource.stop();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
Log.d("Camerapreview", "h = " + h + " w = " + w);
// start preview with new settings
try {
cameraSource.start(holder);
} catch (SecurityException s) {
Log.d(TAG, "shouldn't be here as permission should have been acquired by the main thread");
}
catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
from the camera preview class.
However, the code above leads to RuntimeException for setParameters failed at the line camera.setParameters(params);
. I also tried FOCUS_MODE_CONTINUOUS_PICTURE
but that does not help.