如何有效地重新启动已经采取了preVIEW一个Android应用程序里的照片? [英] How to effectively re-start the preview having taken a photo inside an Android app?

查看:128
本文介绍了如何有效地重新启动已经采取了preVIEW一个Android应用程序里的照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图codeA工具,在一个专用的间隔拍摄照片(如时光倒流)和时遇到困难得到相机重置后的第一​​个捕获,这样它就可以在一张照片开始。

I am attempting to code a tool that takes pictures at a dedicated interval (like a timelapse) and am having difficulty getting the camera to reset following the first capture so that it can then start on the next photo.

下面是使用code的样本:

Here is a sample of the code used:

// gets called with the oncreate method and loads a preview fine
public void startUpPreview(){ 
    mCamera = getCameraInstance();
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
}

// gets called from a later loop
public void getPicture() {
    mCamera.takePicture(null, null, mPicture);
    releaseCamera();
    startUpPreview();
}

通过上面的code,它抛出的错误:

With the code above, it throws the error:

了java.lang.RuntimeException:方法发布之后被称为()

java.lang.RuntimeException: Method called after release()

我使用直接从SDK指南所释放code其中工程否则:

I am using the release code taken directly from the SDK guide which works otherwise:

private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        
        mCamera = null;
    }
}

有关单张照片添加一个休眠线程的延迟使得它的工作:

For a single picture adding a delay with a sleeping thread makes it work:

try {
        Thread.sleep(1000);
        releaseCamera();
        startUpPreview();
} catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
}

但是,这code没有一个循环中尝试拍摄多张图片时工作。我认为环路所有这一切拍照前完成能赶上。

But this code does not work within a loop when attempting to take more than one picture. I assume that the loop is completing before all of that photo taking can catch up.

在此先感谢您的帮助,任何一个可以提供。

Thanks in advance for any help that any one can provide.

推荐答案

takePicture()开始它与(异步)结束捕获过程调用 onPictureTaken()回调。之前,它是在你不应该释放摄像头。

takePicture() starts the capture process which ends with an (asynchronous) call to onPictureTaken() callback. You should not release camera before it is over.

所以伪code,将执行一个循环是这样的:

So the pseudo-code that will perform a loop look like this:

onCLick() { // <-- start the loop
  count = 0;
  takePicture()
}

onPictureTaken() {
  savePicture(count);
  count++;
  if (count < maxCount) {
      mCamera.startPreview();
      mHandler.postDelayed(runTakePicture(), 1000);
  }
  else {
      releaseCamera();
  }
}

runTakePicture() {
   return new Runnable() {
      public void run() {
        mCamera.takePicture();
      }
   }
}

这篇关于如何有效地重新启动已经采取了preVIEW一个Android应用程序里的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆