在Android编程中使用app相机拍摄多张照片

时间:2014-11-17 12:21:58

标签: android camera

我需要将手机的相机用于应用程序,但我是一名新程序员,我无法在任何地方找到完整答案的网站。

这是我的主要活动:

公共类MainActivity扩展了Activity {

   private Camera cameraObject;
   private ShowCamera showCamera;
   public static Camera isCameraAvailiable(){
      Camera object = null;
      try {
         object = Camera.open(); // attempt to get a Camera instance
      }
      catch (Exception e){
        // Camera is not available (in use or does not exist)
      }
      return object; // returns null if camera is unavailable
   }

   private PictureCallback capturedIt = new PictureCallback() {

      @Override
      public void onPictureTaken(byte[] data, Camera camera) {

      Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
      if(bitmap==null){
         Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
      }
      else
      {
         Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();       
      }
      cameraObject.release();
   }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      cameraObject = isCameraAvailiable();
      showCamera = new ShowCamera(this, cameraObject);
      FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
      preview.addView(showCamera);
}

  public void snapIt(View view){
      cameraObject.takePicture(null, null, capturedIt);
   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

2 个答案:

答案 0 :(得分:1)

请检查此链接 http://developer.android.com/training/camera/photobasics.html 它将允许打开设备的默认摄像头。

答案 1 :(得分:0)

创建一个具有布局的片段,以接收正在使用的图像(可以是最后一个的ImageView,带有所有拍摄图片的GridView,w / e对您的应用更有意义)并且"新图片"按钮。然后,您只需将onClickListener添加到Button,再次调用Camera的活动,并将照片添加到您计划存储它们的位置。然后当你拍摄照片时(当拍摄照片时回调内部)并获利时,只需调用片段即可!