我如何在Android中获得连续的相机框架?

时间:2015-06-01 09:05:24

标签: android

我希望使用Android相机拍摄连续拍摄,以使用2张图像获取HDR图像。
但我的代码不起作用。我的应用程序只拍摄了两次没有声音(没有快门声音),也没有保存文件(没有文件) 首先,我认为我的文件名是相同的。所以,我改变了我的文件文件代码,

    // Create a media file name
    // add .SSS for identity
    String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()); 
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");

其次,使用synchronized和startPreview()。但仍然无法正常工作。

下面是我使用"私人相机mCamera"。

的捕获代码
// Add a listener to the Capture button
captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            synchronized (mPicture) {
                    // get an image from the camera
                    Camera.Parameters cp = mCamera.getParameters();
                    cp.setExposureCompensation(cp.getMinExposureCompensation());
                    mCamera.setParameters(cp);
                    mCamera.takePicture(null, null, mPicture);

                    // if i delete below 5 line then work correctly(but, just one image i can get)
                    mCamera.startPreview();

                    cp.setExposureCompensation(cp.getMaxExposureCompensation());
                    mCamera.setParameters(cp);
                    mCamera.takePicture(null, null, mPicture);
                    mCamera.startPreview();
                }
            }
        }
    );

以下是图片回调方法。

private PictureCallback mPicture = new PictureCallback() {
    String TAG = "HDR_TAG";
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};
你能给我一些建议吗? 谢谢。
(对不起,我的英语技能不好......)

2 个答案:

答案 0 :(得分:0)

您可以通过为相机设置previewCallBack来连续从相机获取图像数据。

camera.setPreviewCallback(previewCallback);

private Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera cam) {

     //get the image data here and save it..


    }
};

答案 1 :(得分:0)

我使用线程解决了我的问题而且无休止。见下文。

NSPrivateQueueConcurrencyType

并使用一些标记进行捕获 最后看到{}。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    // Add a listener to the Capture button
    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // get an image from the camera
            Thread t_1 = new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Camera.Parameters cp = mCamera.getParameters();
                    cp.setAutoExposureLock(true);
                    mCamera.takePicture(null, null, mPicture);
                }
            });
            Thread t_2 = new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while (takePicFlag != CAPTURE_SECOND)
                        ;
                    Camera.Parameters cp = mCamera.getParameters();
                    cp.setExposureCompensation(cp
                            .getMinExposureCompensation());
                    mCamera.setParameters(cp);
                    mCamera.takePicture(null, null, mPicture);

                }
            });
            Thread t_3 = new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    while (takePicFlag != CAPTURE_THIRD)
                        ;
                    Camera.Parameters cp = mCamera.getParameters();
                    cp.setExposureCompensation(cp
                            .getMaxExposureCompensation());
                    mCamera.setParameters(cp);
                    mCamera.takePicture(null, null, mPicture);
                }
            });

            t_1.start();
            t_2.start();
            t_3.start();
        }
    });
}