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");
        }

但不幸的是,在完成所有这些之后,静止图像不是全尺寸的,它总是风景。

4

1 回答 1

0

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

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

与:mat.postRotate(角度);

希望我对你有帮助:-)

问候,伍迪。

于 2013-07-25T11:41:59.027 回答