2013-03-01 41 views
0

我要添加图像捕获我的Android应用程序,其中用户可以捕捉图像,我用下面的代码:机器人图像采集总是景观,而不是全尺寸

public void open_camera() { 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 

    try { 
     // place where to store camera taken picture 
     photo = this.createTemporaryFile("picture", ".jpg"); 
     photo.delete(); 
    } catch(Exception e) { 
     Toast.makeText(this, "Please check SD card! Image shot is impossible!", 10000); 
    } 
    mImageUri = Uri.fromFile(photo); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); 
    //start camera intent 
    startActivityForResult(intent,SELECT_PICTURE_CAMERA); 

    //Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    //startActivityForResult(cameraIntent, SELECT_PICTURE_CAMERA); 
} 

private File createTemporaryFile(String part, String ext) throws Exception { 
    File tempDir= Environment.getExternalStorageDirectory(); 
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/"); 
    if(!tempDir.exists()) { 
     tempDir.mkdir(); 
    } 
    return File.createTempFile(part, ext, tempDir); 
} 

当我取回

case SELECT_PICTURE_CAMERA: 
    if(resultCode == RESULT_OK) { 
     // Toast.makeText(this,"Camera" + imageReturnedIntent.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_LONG).show(); 
     //this.yourSelectedImage = (Bitmap) imageReturnedIntent.getExtras().get("data"); 
     /* 
     ImageButton imgbtn = (ImageButton) findViewById(R.id.imageButton_Photo); 
     BitmapDrawable background = new BitmapDrawable(this.yourSelectedImage); 
     imgbtn.setBackgroundDrawable(background); 
     */ 

     try { 
      File f=new File(mImageUri.getPath()); 

      ExifInterface exif = new ExifInterface(f.getPath()); 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

      int angle = 0; 
      if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
       angle = 90; 
      } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
       angle = 180; 
      } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
       angle = 270; 
      } 

      Matrix mat = new Matrix(); 
      mat.postRotate(90); 

      Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
      this.yourSelectedImage = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);     
     } catch (IOException e) { 
      Log.w("TAG", "-- Error in setting image"); 
     } catch(OutOfMemoryError oom) { 
      Log.w("TAG", "-- OOM Error in setting image"); 
     } 

但不幸的是,在做完所有这些之后,静止图像并不是完整的,它总是风景。

回答

0

代码中有一个小错误,在获得图像旋转角度后,您需要在postRotate中使用此值。

请替换行: mat.postRotate(90);

附: mat.postRotate(angle);

希望我帮你:-)

问候, 伍迪。