Android PictureCallback需要先执行然后继续执行其他代码

时间:2014-07-07 21:39:56

标签: java android android-camera

我有代码可以拍摄一张有效的图片,但是在它到达PictureCallback()之后,它继续在代码方面发挥作用,最后在我拥有字节数组之前执行其他代码。有没有办法停止这个?它只需要几毫秒,我不认为我会介意停止UI线程。

private static byte[] patronImage;
.
.
.
takePatronPicture();
//I want to do something with patronImage here, but the next line
//gets executed almost immediately while getJpegCallback is still
//doing its thing.
code that does something with patronImage;
.
.
.
public void takePatronPicture(){
        if (camera != null) {
            try {
              SurfaceView surfaceView = (SurfaceView)getView().findViewById(R.id.surfaceView1);
              camera.setPreviewDisplay(surfaceView.getHolder());    
              camera.startPreview(); 
              camera.takePicture(null, null, getJpegCallback);  
            }
            catch(Exception e) {
                e.printStackTrace();
            }          
        }
    }

    final PictureCallback getJpegCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            try {  
                patronImage = data;

                camera.release();
                camera = null;
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    };

1 个答案:

答案 0 :(得分:1)

回调的目的是它在完成后会打电话给你。如果您要运行需要操作数据的其他代码,请将其放入回调中。您不会冻结当前线程等待它。事实上,如果操作将事件发布到主线程的循环器,那么这样做可能会导致死锁。