0

我正在用安卓相机拍四张照片。在每张照片之后,我希望相机立即预览,然后再拍摄一张。util我有四个。但是当我运行我的应用程序时,它拍摄了第一张照片,然后我听到了四个摄像头的声音。我猜其他照片正在显示但未显示在表面视图上。我怎样才能解决这个问题?

这是我的代码。

public class TakePhoto extends Activity implements SurfaceHolder.Callback, OnClickListener{

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;

    TextView  textView1 ;

    /** Called when the activity is first created. */

    ImageView takepicture;       

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl 
                = new LayoutParams(LayoutParams.FILL_PARENT, 
                LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        takepicture = (ImageView)findViewById(R.id.takepicture);
        takepicture.setOnClickListener(this);

        textView1 = (TextView)findViewById(R.id.textView1);
        textView1.setText("");

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        // TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
        camera.setDisplayOrientation(90);

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()){
            case R.id.takepicture:
                new CountDownTimer(4000, 1000) {

                    public void onTick(long millisUntilFinished) {
                        textView1.setText(" " + millisUntilFinished / 1000);
                    }

                    public void onFinish() {
                        textView1.setText("");

                        //camera.takePicture(myShutterCallback, 
                        //myPictureCallback_RAW, myPictureCallback_JPG);

                        new CountDownTimer(5000, 1000) {

                            public void onTick(long millisUntilFinished) {

                                camera.startPreview();
                                camera.takePicture(myShutterCallback, myPictureCallback_JPG, myPictureCallback_JPG);

                            }

                             public void onFinish() {
                                 textView1.setText("");
                             }    
                         }.start();
                     }   
                 }.start();
                 break;
             }  
         }

         ShutterCallback myShutterCallback = new ShutterCallback(){

            @Override
            public void onShutter() {
                // TODO Auto-generated method stub
            }
         };

         PictureCallback myPictureCallback_RAW = new PictureCallback(){

             @Override
             public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub
             }
         };

         PictureCallback myPictureCallback_JPG = new PictureCallback(){

             @Override
             public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub
                Bitmap bitmapPicture 
                        = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
             }
         };

         private class CaptureThread extends Thread {

            @Override
            public void run() {

                int count = 0;
                while(count < 4) {

                    camera.takePicture(myShutterCallback, myPictureCallback_JPG, myPictureCallback_JPG);

                    count++;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException exception) {
                        exception.printStackTrace();
                    }
                }
            }
        }
    }
}
4

0 回答 0