预览相机框架不捕获Android

时间:2016-01-24 22:25:00

标签: android camera android-camera frame preview

我有代码设置(显然不正确,但似乎没问题)

我捕获帧的测试代码是这样的:

frameCallback = new Camera.PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            System.out.println("preview frame captured");

但是这个onPreviewFrame从未运行过,因为print语句会测试。

奇怪的是,预览在我的表面上完美呈现。即 - 我可以在屏幕上看到它并且没有错误。那么为什么不调用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) {}
    }

2 个答案:

答案 0 :(得分:1)

<img ... alt="click to drag !"><br>
<a href="#">Click to add to cart !</a>

不要在那里调用那个功能。它将在您使用之前停止预览功能。

将以下代码移至refreshCamera(); 并使用width和heigth参数

onSurfaceChanged()

永远不要对像800x480这样的分辨率进行硬编码

答案 1 :(得分:0)

尝试解锁

camera.unlock();
camera.reconnect();
frameCallback.setPreviewCallback(mCallback);