Не вызывается PictureCallback onPictureTaken функции camera.takePicture в Android 12
Есть приложение, где реализован функционал камеры. Кликаю на кнопку происходит снимок фото, все хорошо но на Android 12 эмуляторе (физического устройства android 12 у меня нет) снимок не делается, экран как бы подвисает. Извиняюсь, потому что сильно код не знаю, но продебажив проблему удалось выяснить что не вызывается PictureCallback onPictureTaken функции camera.takePicture вот код этого куска, может кто то может подсказать: 1.
CameraController.PictureCallback pictureCallback = new CameraController.PictureCallback() {
private boolean success = false; // whether jpeg callback succeeded
private boolean has_date = false;
private Date current_date = null;
public void onStarted() {
if( MyDebug.LOG )
Log.d(TAG, "onStarted");
applicationInterface.onCaptureStarted();
if( applicationInterface.getBurstForNoiseReduction() && applicationInterface.getNRModePref() == ApplicationInterface.NRModePref.NRMODE_LOW_LIGHT ) {
if( camera_controller.getBurstTotal() >= CameraController.N_IMAGES_NR_DARK_LOW_LIGHT ) {
showToast(null, R.string.preference_nr_mode_low_light_message);
}
}
}
public void onCompleted() {
if( MyDebug.LOG )
Log.d(TAG, "onCompleted");
applicationInterface.onPictureCompleted();
if( !using_android_l ) {
is_preview_started = false; // preview automatically stopped due to taking photo on original Camera API
}
phase = PHASE_NORMAL; // need to set this even if remaining repeat photos, so we can restart the preview
if( remaining_repeat_photos == -1 || remaining_repeat_photos > 0 ) {
if( !is_preview_started ) {
// we need to restart the preview; and we do this in the callback, as we need to restart after saving the image
// (otherwise this can fail, at least on Nexus 7)
if( MyDebug.LOG )
Log.d(TAG, "repeat mode photos remaining: onPictureTaken about to start preview: " + remaining_repeat_photos);
startCameraPreview();
if( MyDebug.LOG )
Log.d(TAG, "repeat mode photos remaining: onPictureTaken started preview: " + remaining_repeat_photos);
}
applicationInterface.cameraInOperation(false, false);
}
else {
phase = PHASE_NORMAL;
boolean pause_preview = applicationInterface.getPausePreviewPref();
if( MyDebug.LOG )
Log.d(TAG, "pause_preview? " + pause_preview);
if( pause_preview && success ) {
if( is_preview_started ) {
// need to manually stop preview on Android L Camera2
if( camera_controller != null ) {
camera_controller.stopPreview();
}
is_preview_started = false;
}
setPreviewPaused(true);
}
else {
if( !is_preview_started ) {
// we need to restart the preview; and we do this in the callback, as we need to restart after saving the image
// (otherwise this can fail, at least on Nexus 7)
startCameraPreview();
}
applicationInterface.cameraInOperation(false, false);
if( MyDebug.LOG )
Log.d(TAG, "onPictureTaken started preview");
}
}
continuousFocusReset(); // in case we took a photo after user had touched to focus (causing us to switch from continuous to autofocus mode)
if( camera_controller != null && focus_value != null && ( focus_value.equals("focus_mode_continuous_picture") || focus_value.equals("focus_mode_continuous_video") ) ) {
if( MyDebug.LOG )
Log.d(TAG, "cancelAutoFocus to restart continuous focusing");
camera_controller.cancelAutoFocus(); // needed to restart continuous focusing
}
if( camera_controller != null && camera_controller.getBurstType() == CameraController.BurstType.BURSTTYPE_CONTINUOUS ) {
if( MyDebug.LOG )
Log.d(TAG, "continuous burst mode ended, so revert to standard mode");
setupBurstMode();
}
if( MyDebug.LOG )
Log.d(TAG, "do we need to take another photo? remaining_repeat_photos: " + remaining_repeat_photos);
if( remaining_repeat_photos == -1 || remaining_repeat_photos > 0 ) {
takeRemainingRepeatPhotos();
}
}
/** Ensures we get the same date for both JPEG and RAW; and that we set the date ASAP so that it corresponds to actual
* photo time.
*/
private void initDate() {
if( !has_date ) {
has_date = true;
current_date = new Date();
if( MyDebug.LOG )
Log.d(TAG, "picture taken on date: " + current_date);
}
}
public void onPictureTaken(byte[] data) {
if( MyDebug.LOG )
Log.d(TAG, "onPictureTaken");
// n.b., this is automatically run in a different thread
initDate();
if( !applicationInterface.onPictureTaken(data, current_date) ) {
if( MyDebug.LOG )
Log.e(TAG, "applicationInterface.onPictureTaken failed");
success = false;
}
else {
success = true;
}
}
public void onRawPictureTaken(RawImage raw_image) {
if( MyDebug.LOG )
Log.d(TAG, "onRawPictureTaken");
initDate();
if( !applicationInterface.onRawPictureTaken(raw_image, current_date) ) {
if( MyDebug.LOG )
Log.e(TAG, "applicationInterface.onRawPictureTaken failed");
}
}
public void onBurstPictureTaken(List<byte[]> images) {
if( MyDebug.LOG )
Log.d(TAG, "onBurstPictureTaken");
// n.b., this is automatically run in a different thread
initDate();
success = true;
if( !applicationInterface.onBurstPictureTaken(images, current_date) ) {
if( MyDebug.LOG )
Log.e(TAG, "applicationInterface.onBurstPictureTaken failed");
success = false;
}
}
public boolean imageQueueWouldBlock(int n_jpegs) {
if( MyDebug.LOG )
Log.d(TAG, "imageQueueWouldBlock");
return applicationInterface.imageQueueWouldBlock(false, n_jpegs);
}
public void onFrontScreenTurnOn() {
if( MyDebug.LOG )
Log.d(TAG, "onFrontScreenTurnOn");
applicationInterface.turnFrontScreenFlashOn();
}
};
private void takePictureNow(final CameraController.PictureCallback picture, final ErrorCallback error) {
if( MyDebug.LOG )
Log.d(TAG, "takePictureNow");
// only set the shutter callback if sounds enabled
final Camera.ShutterCallback shutter = sounds_enabled ? new TakePictureShutterCallback() : null;
final Camera.PictureCallback camera_jpeg = picture == null ? null : new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera cam) {
if( MyDebug.LOG )
Log.d(TAG, "onPictureTaken");
// n.b., this is automatically run in a different thread
if( want_expo_bracketing && n_burst > 1 ) {
pending_burst_images.add(data);
if( pending_burst_images.size() >= n_burst ) { // shouldn't ever be greater, but just in case
if( MyDebug.LOG )
Log.d(TAG, "all burst images available");
if( pending_burst_images.size() > n_burst ) {
Log.e(TAG, "pending_burst_images size " + pending_burst_images.size() + " is greater than n_burst " + n_burst);
}
// set exposure compensation back to original
setExposureCompensation(burst_exposures.get(0));
// take a copy, so that we can clear pending_burst_images
// also allows us to reorder from dark to light
// since we took the images with the base exposure being first
int n_half_images = pending_burst_images.size()/2;
List<byte []> images = new ArrayList<>();
// darker images
for(int i=0;i<n_half_images;i++) {
images.add(pending_burst_images.get(i+1));
}
// base image
images.add(pending_burst_images.get(0));
// lighter images
for(int i=0;i<n_half_images;i++) {
images.add(pending_burst_images.get(n_half_images+1));
}
picture.onBurstPictureTaken(images);
pending_burst_images.clear();
picture.onCompleted();
}
else {
if( MyDebug.LOG )
Log.d(TAG, "number of burst images is now: " + pending_burst_images.size());
// set exposure compensation for next image
setExposureCompensation(burst_exposures.get(pending_burst_images.size()));
// need to start preview again: otherwise fail to take subsequent photos on Nexus 6
// and Nexus 7; on Galaxy Nexus we succeed, but exposure compensation has no effect
try {
startPreview();
}
catch(CameraControllerException e) {
if( MyDebug.LOG )
Log.d(TAG, "CameraControllerException trying to startPreview");
e.printStackTrace();
}
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
if( MyDebug.LOG )
Log.d(TAG, "take picture after delay for next expo");
if( camera != null ) { // make sure camera wasn't released in the meantime
takePictureNow(picture, error);
}
}
}, 1000);
}
}
else {
picture.onPictureTaken(data);
picture.onCompleted();
}
}
};
if( picture != null ) {
if( MyDebug.LOG )
Log.d(TAG, "call onStarted() in callback");
picture.onStarted();
}
try {
camera.takePicture(shutter, null, camera_jpeg);
}
catch(RuntimeException e) {
// just in case? We got a RuntimeException report here from 1 user on Google Play; I also encountered it myself once of Galaxy Nexus when starting up
if( MyDebug.LOG )
Log.e(TAG, "runtime exception from takePicture");
e.printStackTrace();
error.onError();
}
}
Если что camera это обьект Camera