在三星手机上拍照失败

Alf*_*and 5 camera android samsung-mobile xamarin

我正在使用Xamarin编写适用于Android的应用,该应用具有自定义活动,该活动用于使用CameraAPI 捕获图片。该活动在我测试过的所有设备上均能完美运行,但是一些用户报告说,在尝试拍照时,应用程序完全崩溃了。很快很明显,所有这些用户都在使用三星手机,但是不幸的是,我没有一个可以进行测试的手机。

值得庆幸的是,我已经能够捕获异常和堆栈跟踪,但是我对可能导致此问题的原因不知所措。异常,堆栈跟踪和有问题的代码如下。

这是一个相当简单的活动,具有全屏相机预览,闪光灯切换和拍摄按钮。它使用一个自定义CameraHelper类来设置CameraAPI 并与之交互。OnSurfaceTextureAvailable在用户能够进行交互并触发该TakePicture方法之前,通过该方法配置摄像头并显示预览。

异常堆栈跟踪

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
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

相机可用,并且预览显示没有问题,并且仅在Samsung设备上引发了异常。

Alf*_*and 2

当三星 Galaxy 手机第一次自动对焦失败时,会引发异常 - 虽然大多数设备只会尝试对焦一次,但三星 Galaxy 手机会重新尝试最多 3 次,并在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
}
Run Code Online (Sandbox Code Playgroud)