android图像捕获总是风景而不是全尺寸

时间:2013-03-01 15:24:02

标签: android image screen-capture

我想在我的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");
        }

但遗憾的是,在完成所有这些操作后,静止图像的尺寸并不完整,并且始终是风景图。

1 个答案:

答案 0 :(得分:0)

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

请更换线路: mat.postRotate(90);

使用: mat.postRotate(角度);

希望我帮助过你: - )

此致 木质。