Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android PictureCallback需要先执行,然后继续执行其他代码_Java_Android_Android Camera - Fatal编程技术网

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

Java Android PictureCallback需要先执行,然后继续执行其他代码,java,android,android-camera,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

我有代码来拍摄一张有效的图片,但是在它到达
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();
            }
        }
    };

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

谢谢。有时我看不见森林,看不见树木。