相机预览没有重启?

时间:2012-06-06 11:20:22

标签: android android-intent android-camera

我正在使用android中的相机代码拍照并将其保存在手机上。它从手机摄像头拍摄照片并将其保存在存储卡上。唯一的问题是拍照后相机预览没有重新启动。

我无法弄清楚解决方案。代码如下。建议是必要的。 。 。 我的项目中有两个类。 。 。

CAMERAACTIVITY CLASS

public class CameraActivity extends Activity 
{

  private static final String TAG = "CameraDemo";
  Preview preview; 
  Button buttonClick; 

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    preview = new Preview(this); 
    ((FrameLayout) findViewById(R.id.preview)).addView(preview); 

    buttonClick = (Button) findViewById(R.id.buttonClick);
    buttonClick.setOnClickListener(new OnClickListener() {
      public void onClick(View v) { 
        preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);    
      }
    });

    Log.d(TAG, "onCreate'd");
  }

  // Called when shutter is opened
  ShutterCallback shutterCallback = new ShutterCallback() { 
    public void onShutter() {
      Log.d(TAG, "onShutter'd");
    }
  };

  // Handles data for raw picture
  PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      Log.d(TAG, "onPictureTaken - raw");
    }
  };

  // Handles data for jpeg picture
  PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
      try {
        // Write to SD Card
        outStream = new FileOutputStream(String.format("/sdcard/DCIM/queries.jpg",
            System.currentTimeMillis()));
        outStream.write(data);
        outStream.close();
        Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
      } catch (FileNotFoundException e) { 
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
      }
      Log.d(TAG, "onPictureTaken - jpeg");

    }
  };

}

预览课程

class Preview extends SurfaceView implements SurfaceHolder.Callback{ 
  private static final String TAG = "Preview";

  SurfaceHolder mHolder;  // <2>
  public Camera camera; // <3>

  Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();  // <4>
    mHolder.addCallback(this);  // <5>
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>

  }

  // Called once the holder is ready
  public void surfaceCreated(SurfaceHolder holder) {  // <7>
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    camera = Camera.open(); // <8>
    try {


        Camera.Parameters parameters = camera.getParameters();
        parameters.set("orientation", "landscape");
        camera.setParameters(parameters);
        camera.setPreviewDisplay(holder);  

        camera.setPreviewCallback(new PreviewCallback() { 
        // Called for each frame previewed
        public void onPreviewFrame(byte[] data, Camera camera) {  
          Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
          Preview.this.invalidate();  
        }
      });
    } catch (IOException e) { 
      e.printStackTrace();
    }

  }

  // Called when the holder is destroyed
  public void surfaceDestroyed(SurfaceHolder holder) { 
    //Log.d(TAG,"Stopping preview in SurfaceDestroyed().");
    camera.setPreviewCallback(null);
    camera.stopPreview();
    camera.release();
    camera = null;           
  }

  // Called when holder has changed
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    camera.startPreview();
  }

}

3 个答案:

答案 0 :(得分:12)

The article about Camera from Android API Guide也遇到同样的问题。我可以通过添加以下两个更多的内容来拍摄预览:

1)在AndroidManifest.xml文件中添加外部存储权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2)通过调用camera.startPreview()再次开始预览。在您的代码中:

...
Log.d(TAG, "onPictureTaken - jpeg");
camera.startPreview();
....

我相信你能够以同样的方式让你的工作。

答案 1 :(得分:2)

在onPictureTaken中再次启动CAMERAACTIVITY,如下所示:

// Handles data for jpeg picture
  PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
      try {
        // Write to SD Card
        outStream = new FileOutputStream(String.format("/sdcard/DCIM/queries.jpg",
            System.currentTimeMillis()));
        outStream.write(data);
        outStream.close();
        Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
      } catch (FileNotFoundException e) { 
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        camera.release();
        camera = null;
        startActivity(new Intent(CAMERAACTIVITY.this, CAMERAACTIVITY.class));
        finish();
      }
      Log.d(TAG, "onPictureTaken - jpeg");

    }
  };

如果需要,还可以使用Thread.sleep(2000);这样,捕获的图像将显示2秒钟,然后再次开始摄像机活动。

答案 2 :(得分:0)

添加 Thread.sleep代码(2000); camera.startPreview();

这样摄像机将显示捕获的图像2秒钟并重新启动摄像机。