[Java for Android App Development] colt2x:
Hi,

I have built a small notepad+barcodescanner app. Not it works, but the performance is low, as it processes all frames. It would be good if it would process only frams which are captured when the focus settled.

The app uses Camera2 PreviewBulder and CaptureRequestBuilder / RepeatingRequest, but i only found ways to get focus state during a Capturesession. (Capture is not used in this app, only getting frames from preview).

Does anyone how to process the focus state if one uses a Preview...?

Thanks for any help :)

Corresponding code part:

private final CameraDevice.StateCallback stateCallback = new

CameraDevice.StateCallback() {

@override

public void onOpened(CameraDevice camera) {

//This is called when the camera is open

// Log.e(TAG, "onOpened");

cameraDevice = camera;

createCameraPreview();

}

@override

public void onDisconnected(CameraDevice camera) {

cameraDevice.close();

}

@override

public void onError(CameraDevice camera, int error) {

cameraDevice.close();

cameraDevice = null;

}

};

final CameraCaptureSession.CaptureCallback captureCallbackListener = new CameraCaptureSession.CaptureCallback() {

@override

public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {

super.onCaptureCompleted(session, request, result);

// makeText(MainActivity.this, "Saved:" + file, LENGTH_SHORT).show();

createCameraPreview();

}

};

protected void startBackgroundThread() {

mBackgroundThread = new HandlerThread("Camera Background");

mBackgroundThread.start();

mBackgroundHandler = new Handler(mBackgroundThread.getLooper());

}

protected void stopBackgroundThread() {

mBackgroundThread.quitSafely();

try {

mBackgroundThread.join();

mBackgroundThread = null;

mBackgroundHandler = null;

} catch (InterruptedException e) {

e.printStackTrace();

}

}

protected void createCameraPreview() {

try {

SurfaceTexture texture = textureView.getSurfaceTexture();

assert texture != null;

texture.setDefaultBufferSize(imageDimension.getWidth(),

imageDimension.getHeight());

Surface surface = new Surface(texture);

CameraManager manager;

manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

try {

String camerId = manager.getCameraIdList()[0];

CameraCharacteristics characteristics = manager.getCameraCharacteristics(camerId);

boolean aelockavailable = characteristics.get(CameraCharacteristics.CONTROL_AE_LOCK_AVAILABLE);

}catch (Exception e)

{

}

captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, captureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

//captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, captureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

captureRequestBuilder.addTarget(surface);

cameraDevice.createCaptureSession(Arrays.asList(surface),

new CameraCaptureSession.StateCallback() {

@override

public void onConfigured(@NonNull CameraCaptureSession

cameraCaptureSession) {

//The camera is already closed

if (null == cameraDevice) {

return;

}

// When the session is ready, we start displaying the preview.

cameraCaptureSessions = cameraCaptureSession;

updatePreview();

}

@override

public void onConfigureFailed(@NonNull

CameraCaptureSession cameraCaptureSession) {

//makeText(MainActivity.this, "Configuration change", LENGTH_SHORT).show();

}

}, null);

} catch (CameraAccessException e) {

e.printStackTrace();

}

}

void openCamera() {

CameraManager manager = (CameraManager)

getSystemService(Context.CAMERA_SERVICE);

//Log.e(TAG, "is camera open");

try {

cameraId = manager.getCameraIdList()[0];

CameraCharacteristics characteristics =

manager.getCameraCharacteristics(cameraId);

StreamConfigurationMap map =

characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

assert map != null;

imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];

// Add permission for camera and let user grant the permission

if (ActivityCompat.checkSelfPermission(this,

Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&

ActivityCompat.checkSelfPermission(this,

Manifest.permission.WRITE_EXTERNAL_STORAGE) !=

PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this,

new String[]{Manifest.permission.CAMERA,

Manifest.permission.WRITE_EXTERNAL_STORAGE},

REQUEST_CAMERA_PERMISSION);

return;

}

manager.openCamera(cameraId, stateCallback, null);

} catch (CameraAccessException e) {

e.printStackTrace();

}

//Log.e(TAG, "openCamera X");

}

void updatePreview() {

if (null == cameraDevice) {

//Log.e(TAG, "updatePreview error, return");

}

captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);

captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, captureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

//captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

if (flashchanged) {flashchanged=false; if (lamp && autoflash) {captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);} else {captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);} }

try {

cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(),

null, mBackgroundHandler);

} catch (CameraAccessException e) {

e.printStackTrace();

}

}

private void closeCamera() {

if (null != cameraDevice) {

cameraDevice.close();

cameraDevice = null;

}

if (null != imageReader) {

imageReader.close();

imageReader = null;

}

}