三星手机拍照失败
Take picture fails on Samsung phones
我正在使用 Xamarin 为 Android 编写一个应用程序,它具有自定义 activity 用于使用 Camera
API 捕获图片。这个 activity 在我测试过的所有设备上都能完美运行,但是一些用户报告说在尝试拍照时应用程序完全崩溃了。很快就可以看出所有这些用户都在使用三星手机,不幸的是我没有可以测试的手机。
谢天谢地,我已经能够捕获异常和堆栈跟踪,但我不知道是什么导致了这个问题。异常、堆栈跟踪和有问题的代码如下。
这是一个相当简单的 activity,带有全屏相机预览、闪光灯切换和拍摄按钮。它使用自定义 CameraHelper
class 来设置 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);
}
摄像头可用,预览显示没有问题,只有三星设备才会抛出异常。
当 Samsung Galaxy phone 第一次自动对焦失败时抛出异常 - 虽然大多数设备只会尝试对焦一次,但 Samsung Galaxy phone 最多会重新尝试三次次,并在每次尝试后调用 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
}
我正在使用 Xamarin 为 Android 编写一个应用程序,它具有自定义 activity 用于使用 Camera
API 捕获图片。这个 activity 在我测试过的所有设备上都能完美运行,但是一些用户报告说在尝试拍照时应用程序完全崩溃了。很快就可以看出所有这些用户都在使用三星手机,不幸的是我没有可以测试的手机。
谢天谢地,我已经能够捕获异常和堆栈跟踪,但我不知道是什么导致了这个问题。异常、堆栈跟踪和有问题的代码如下。
这是一个相当简单的 activity,带有全屏相机预览、闪光灯切换和拍摄按钮。它使用自定义 CameraHelper
class 来设置 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);
}
摄像头可用,预览显示没有问题,只有三星设备才会抛出异常。
当 Samsung Galaxy phone 第一次自动对焦失败时抛出异常 - 虽然大多数设备只会尝试对焦一次,但 Samsung Galaxy phone 最多会重新尝试三次次,并在每次尝试后调用 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
}