预览相机帧未捕获 Android
Preview camera frame not capturing Android
我有代码设置(显然不正确但看起来没问题)
我的抓帧测试代码是这样的:
frameCallback = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
System.out.println("preview frame captured");
但是这个 onPreviewFrame
永远不会 运行,正如打印语句测试的那样。
奇怪的是,预览在我的界面上完美显示。即-我可以在屏幕上看到它并且没有错误。为什么 onPreviewFrame
没有被调用?
以下是我的全部代码供参考:
public class HuntActivity extends AppCompatActivity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera.PreviewCallback frameCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hunt);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
frameCallback = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
System.out.println("preview frame captured");
}
};
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {camera = Camera.open();
} catch (RuntimeException e) {
System.err.println(e);
return;
}
camera.setPreviewCallback(frameCallback);
Camera.Parameters param;
param = camera.getParameters();
param.setPreviewSize(800, 480);
camera.setDisplayOrientation(90);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
System.err.println(e);
return;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
refreshCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {return;}
try {camera.stopPreview();
} catch (Exception e){}
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {}
}
refreshCamera();
不要在那里调用那个函数。它会在您使用之前停止预览功能。
将以下代码移至 onSurfaceChanged()
并使用 width 和 heigth 参数
Camera.Parameters param;
param = camera.getParameters();
//param.setPreviewSize(800, 480);
param.setPreviewSize(width, height);
... and the rest of those lines...
切勿对 800x480 等分辨率进行硬编码
尝试解锁
camera.unlock();
camera.reconnect();
frameCallback.setPreviewCallback(mCallback);
我有代码设置(显然不正确但看起来没问题)
我的抓帧测试代码是这样的:
frameCallback = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
System.out.println("preview frame captured");
但是这个 onPreviewFrame
永远不会 运行,正如打印语句测试的那样。
奇怪的是,预览在我的界面上完美显示。即-我可以在屏幕上看到它并且没有错误。为什么 onPreviewFrame
没有被调用?
以下是我的全部代码供参考:
public class HuntActivity extends AppCompatActivity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera.PreviewCallback frameCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hunt);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
frameCallback = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
System.out.println("preview frame captured");
}
};
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {camera = Camera.open();
} catch (RuntimeException e) {
System.err.println(e);
return;
}
camera.setPreviewCallback(frameCallback);
Camera.Parameters param;
param = camera.getParameters();
param.setPreviewSize(800, 480);
camera.setDisplayOrientation(90);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
System.err.println(e);
return;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
refreshCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {return;}
try {camera.stopPreview();
} catch (Exception e){}
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {}
}
refreshCamera();
不要在那里调用那个函数。它会在您使用之前停止预览功能。
将以下代码移至 onSurfaceChanged()
并使用 width 和 heigth 参数
Camera.Parameters param;
param = camera.getParameters();
//param.setPreviewSize(800, 480);
param.setPreviewSize(width, height);
... and the rest of those lines...
切勿对 800x480 等分辨率进行硬编码
尝试解锁
camera.unlock();
camera.reconnect();
frameCallback.setPreviewCallback(mCallback);