android – 在三星手机上拍照失败
作者:互联网
我正在使用Xamarin为Android编写应用程序,该应用程序具有用于使用Camera API捕获图片的自定义活动.此活动适用于我测试的所有设备,但有些用户在尝试拍照时报告应用程序完全崩溃.很明显,所有这些用户都在使用三星手机,不幸的是我没有可用于测试的用户.
值得庆幸的是,我已经能够捕获异常和堆栈跟踪,但我不知道可能导致此问题的原因.下面是异常,堆栈跟踪和有问题的代码.
这是一个相当简单的活动,具有全屏相机预览,闪光切换和捕捉按钮.它使用自定义CameraHelper类来设置Camera API并与之交互.配置相机并在用户能够交互并触发TakePicture方法之前,通过OnSurfaceTextureAvailable方法显示预览.
异常堆栈跟踪
java.lang.RuntimeException: takePicture failed
android.hardware.Camera.native_takePicture(Native Method):0
android.hardware.Camera.takePicture(Camera.java:1523):0
android.hardware.Camera.takePicture(Camera.java:1468):0
md5efa7d89b8a471e1a97a183b83296df21.CameraHelper.n_onAutoFocus(Native Method):0
md5efa7d89b8a471e1a97a183b83296df21.CameraHelper.onAutoFocus(CameraHelper.java:39):0
CameraHelper中的方法
// Implements Camera.IPictureCallback and Camera.IAutoFocusCallback
public void OnSurfaceTextureAvailable(object sender, TextureView.SurfaceTextureAvailableEventArgs e)
{
// Get the camera and set its orientation
try
{
_camera = Camera.Open(_cameraInt);
}
catch (Exception ex)
{
_callback.OnInitializationFailed(ex);
return;
}
var orientation = GetDisplayOrientation();
_camera.SetDisplayOrientation(orientation);
// Set the camera parameters
var cameraParameters = _camera.GetParameters();
if (cameraParameters.SupportedFocusModes != null && cameraParameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
cameraParameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
if (cameraParameters.SupportedFlashModes != null && cameraParameters.SupportedFlashModes.Contains(Camera.Parameters.FlashModeAuto))
{
cameraParameters.FlashMode = Camera.Parameters.FlashModeAuto;
HasFlash = true;
}
cameraParameters.JpegQuality = JPEG_QUALITY;
// Set the picture resolution
var pictureSize = GetIdealPictureSize(cameraParameters.SupportedPictureSizes, MAX_MEGAPIXELS);
_imageWidth = pictureSize.Width;
_imageHeight = pictureSize.Height;
cameraParameters.SetPictureSize(pictureSize.Width, pictureSize.Height);
// Set the preview resolution to best match the TextureView
var previewSize = GetIdealPreviewSize(cameraParameters.SupportedPreviewSizes, _previewTexture.Height, _previewTexture.Width);
cameraParameters.SetPreviewSize(previewSize.Width, previewSize.Height);
// Begin outputting camera preview
_camera.SetParameters(cameraParameters);
_camera.SetPreviewTexture(_previewTexture.SurfaceTexture);
_camera.StartPreview();
UpdatePreviewTextureMatrix(); // Ensure the preview is displayed without warping
// Wait for the preview
EventHandler<TextureView.SurfaceTextureUpdatedEventArgs> h = null;
_previewTexture.SurfaceTextureUpdated += h = (s, e2) =>
{
_previewTexture.SurfaceTextureUpdated -= h;
_callback.OnCameraPreviewReady();
_ready = true;
};
}
public void TakePicture()
{
if (!_ready || _busy)
{
var e = new Exception("Camera not ready");
OnTakePictureFailed(e);
return;
}
_busy = true;
_camera.AutoFocus(this);
}
public void OnAutoFocus(bool success, Camera camera)
{
try
{
_camera.TakePicture(null, null, this);
}
catch (Exception e)
{
// On Samsung phones the exception is always thrown here
OnTakePictureFailed(e);
}
}
public void OnPictureTaken(byte[] data, Camera camera)
{
_busy = false;
var rotation = GetPictureRotation();
_callback.OnPictureTaken(data, rotation, _imageWidth, _imageHeight);
}
private void OnTakePictureFailed(Exception e)
{
_busy = false;
_callback.OnTakePictureFailed(e);
}
相机可用,预览显示没有问题,只有三星设备才会出现异常.
解决方法:
当三星Galaxy手机第一次无法自动对焦时会抛出异常 – 而大多数设备只会尝试聚焦一次,三星Galaxy手机重新尝试最多三次,并在每次尝试后调用OnAutoFocus回调.因为我的代码在回调中称为Camera.TakePicture,它可以快速连续调用两次或更多次,因此它会尝试在拍摄照片时拍照并抛出异常.
解决方案只是添加一个布尔值来跟踪自动对焦回调是否已经发生,如果是,则跳过调用Camera.TakePicture:
public void OnAutoFocus(bool success, Camera camera)
{
if (_hasAttemptedFocus) return;
else _hasAttemptedFocus = true;
_camera.TakePicture(null, null, this);
}
public void OnPictureTaken(byte[] data, Camera camera)
{
_busy = _hasAttemptedFocus = false;
// Do something with the image
}
标签:android,camera,xamarin,samsung-mobile 来源: https://codeday.me/bug/20190702/1354992.html