在某些设备上未调用Camera onPreviewFrame [英] Camera onPreviewFrame not called on some devices

查看:126
本文介绍了在某些设备上未调用Camera onPreviewFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望LogCat可以多次记录#onPreviewFrame(),但是它只能在以下选定的设备上运行:

I am expecting the LogCat to be logging #onPreviewFrame() multiple times, but it only works on selected devices like:

  • 三星Galaxy S6(7.0)
  • 三星Galaxy S6(6.0.1)
  • LG Leon(5.0.1)

但不能在以下设备上使用:

But does not on the following devices:

  • LG G4(6.0)
  • 华为6X(7.0)
  • Nexus 6P(7.0)

下面是代码段:

public CameraSurfaceView(Context context, AttributeSet set) {
  super(context, set);
  Log.d(TAG, "CameraSurfaceView(context, set)");

  // Get the Surface Holder
  this.holder = this.getHolder();
  this.holder.addCallback(this);
  this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
  try {
    // Turn on the Camera
    this.camera = Camera.open();
  } catch (Exception ex) {
    Log.e(TAG, "#surfaceCreated() error=" + ex.getMessage(), ex);
  }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  Log.d(TAG, "#surfaceChanged()");

  if (holder.getSurface() == null) {
    // preview surface does not exist
    return;
  }
  if (camera == null) {
    // camera does not exist
    return;
  }

  // This allows us to make our own draw calls to this canvas
  this.setWillNotDraw(false);
  // Initialize canvas variables
  previewPaint = new Paint();
  // Initialize preview variables
  deviceWidth = width;
  deviceHeight = height;
  Camera.Size previewSize = this.camera.getParameters().getPreviewSize();
  previewWidth = previewSize.width;
  previewHeight = previewSize.height;
  previewBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);

  // Initialize Scale Variables
  previewScaleMatrix = new Matrix();
  previewScaleMatrix.setScale(deviceWidth / (float) previewWidth,
      deviceHeight / (float) previewHeight, deviceWidth / 2.0f, deviceHeight / 2.0f);

  // Initialize RenderScript variables
  rs = RenderScript.create(getContext());

  // RenderScript YUV to RGB
  yuvToRgbScript = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
  Type.Builder previewRgbaType =
      new Type.Builder(rs, Element.RGBA_8888(rs)).setX(previewWidth).setY(previewHeight);
  yuvToRgbOut = Allocation.createTyped(rs, previewRgbaType.create(), Allocation.USAGE_SCRIPT);

  // RenderScript Invert
  invertScript = new ScriptC_invert(rs);
  invertOut = Allocation.createTyped(rs, yuvToRgbOut.getType(), Allocation.USAGE_SCRIPT);

  // Set color effect to none
  parameters.setColorEffect(Camera.Parameters.EFFECT_NONE);
  camera.setParameters(parameters);

  // Set the preview callback
  Log.d(TAG, "#surfaceChanged() camera.setPreviewCallback()");
  camera.setPreviewCallback(new Camera.PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
      Log.d(TAG, "#onPreviewFrame()");
    }
  });

  // Start the camera preview
  Log.d(TAG, "#surfaceChanged() camera.startPreview()");
  camera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
  // Surface will be destroyed when replaced with a new screen
  // Always make sure to release the Camera instance
  if (camera != null) {
    camera.setPreviewCallback(null);
    camera.stopPreview();
    camera.release();
    camera = null;
  }
}

我的LogCat显示如下:

My LogCat displays as follows:

D/CameraSurfaceView: #surfaceChanged()
D/CameraSurfaceView: #surfaceChanged() camera.setPreviewCallback()
D/CameraSurfaceView: #surfaceChanged() camera.startPreview()

ScriptC_invert来自 https://developer.android.com/guide/topics/renderscript/compute.html#writing-an-rs-kernel

即使我删除了所有RenderScript内容,也存在相同的问题.

And even if I remove all RenderScript stuff, the same problem.

我也尝试了以下方法:

  • Camera onPreviewFrame not called
  • Android onPreviewFrame is not called

注意:

一个修补程序将需要在我的surfaceChanged()方法中使用camera.setPreviewDisplay(holder).但这会给我带来另一个问题,因为我需要在onPreviewFrame()中执行holder.lockCanvas()并遇到以下异常:

One fix would need a camera.setPreviewDisplay(holder) in my surfaceChanged() method. But this will create another issue for me since I would need to do a holder.lockCanvas() in onPreviewFrame() and encounter the following exception:

E/SurfaceHolder: Exception locking surface
                 java.lang.IllegalArgumentException
                     at android.view.Surface.nativeLockCanvas(Native Method)
                     at android.view.Surface.lockCanvas(Surface.java:264)
                     at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:842)
                     at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:830)
                     at com.arcanys.ar.CameraSurfaceView.onPreviewFrame(CameraSurfaceView.java:204)

可能有些东西我遗漏或配置错误.

There might be some things that I've missed or misconfigured.

推荐答案

就目前而言,我没有找到解决此问题的方法,而是有一种解决方法.

As for the time being, I haven't found a fix for this issue, rather I have a workaround.

由于我们不能同时使用camera.setPreviewDisplay(surfaceHolder)camera.setPreviewCallback(previewCallback)并期望在某些设备上使用onPreviewFrame()中的surfaceHolder.lockCanvas(),因此我已将预览转移到另一个SurfaceView并处理了onPreviewFrame()从那里使用自己的SufraceHolder.

Since we cannot use both camera.setPreviewDisplay(surfaceHolder) and camera.setPreviewCallback(previewCallback) at the same time expect to use the surfaceHolder.lockCanvas() in onPreviewFrame() for some devices, I have transferred my preview to another SurfaceView and handle the onPreviewFrame() from there and use its own SufraceHolder.

这篇关于在某些设备上未调用Camera onPreviewFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆