code旋转图像通过摄像头的意图不工作在Android的捕获 [英] code to rotate image captured by camera intent not working in Android

查看:88
本文介绍了code旋转图像通过摄像头的意图不工作在Android的捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我使用的相机的意图拍摄的图像被旋转,这是我问的<一个href="http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android">why使用相机的意图拍摄的图像被在安卓一些设备旋转

I have a problem that my image captured using camera intent gets rotated, which I asked at why image captured using camera intent gets rotated on some devices in android

所以,按照建议的答案的,我现在想转,我开始使用相机(我用的相机意图)的图像。我的code是,

So as per the suggested answers there, I'm now trying to rotate the image which I get using camera(I have used camera intent). My code is,

public class SimpleCameraActivity extends Activity {
private static final int TAKE_PICTURE = 1888;
private Uri imageUri;
ImageView imageView;
Bitmap bitmap;
Button btnOK, btnDiscard;
RelativeLayout rLButtons;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_camera);
    imageView = (ImageView) findViewById(R.id.image_view_sc);
    captureImage();
}

public void captureImage() {
    Intent intentCamera = new Intent("android.media.action.IMAGE_CAPTURE");
    File filePhoto = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
    imageUri = Uri.fromFile(filePhoto);
    MyApplicationGlobal.imageUri = imageUri.getPath();
    intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intentCamera, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentFromCamera) {
    super.onActivityResult(requestCode, resultCode, intentFromCamera);
    if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
        if (intentFromCamera != null) {
            Bundle extras = intentFromCamera.getExtras();
            if (extras.containsKey("data")) {
                bitmap = (Bitmap) extras.get("data");
            } else {
                bitmap = getBitmapFromUri();
            }
        } else {
            bitmap = getBitmapFromUri();
        }
        checkForRotation();
        //rotateImage();
        //rotateImage1();
        rotateImage3();
        //imageView.setImageBitmap(bitmap);
        // imageView.setImageURI(imageUri);
    } else {
        finish();
    }
}

public void rotateImage() {
    Bitmap targetBitmap = Bitmap.createBitmap(100, 80, bitmap.getConfig());
    Canvas canvas = new Canvas(targetBitmap);
    Matrix matrix = new Matrix();
    matrix.setRotate(180, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    matrix.setRotate(90);
    canvas.drawBitmap(bitmap, matrix, new Paint());
    imageView.setImageBitmap(bitmap);
}

public void rotateImage1() {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    matrix.mapRect(rectF);
    Bitmap targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), bitmap.getConfig());
    Canvas canvas = new Canvas(targetBitmap);
    canvas.drawBitmap(bitmap, matrix, new Paint());
    imageView.setImageBitmap(bitmap);
}

public void rotateImage2() {
    Matrix mtx = new Matrix();
    mtx.reset();
    mtx.preTranslate(20, 30);
    mtx.setRotate((float) 90, 25, 35);
    mtx.postTranslate(30, 40);
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, 100, 100, mtx, true);
    this.bitmap = rotatedBMP;
    imageView.setImageBitmap(bitmap);
}

public void rotateImage3() {
    Bitmap bmpOriginal = bitmap;
    Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()), (bmpOriginal.getHeight()), Bitmap.Config.ARGB_8888);
    Paint p = new Paint();
    p.setAntiAlias(true);

    Matrix matrix = new Matrix();
    matrix.setRotate((float) 90, (float) (bmpOriginal.getWidth() / 2), (float) (bmpOriginal.getHeight() / 2));

    RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
    matrix.mapRect(rectF);

    targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), Bitmap.Config.ALPHA_8);

    Canvas tempCanvas = new Canvas(targetBitmap);
    tempCanvas.drawBitmap(bmpOriginal, matrix, p);
    imageView.setImageBitmap(bmpOriginal);
}

public Bitmap getBitmapFromUri() {
    getContentResolver().notifyChange(imageUri, null);
    ContentResolver cr = getContentResolver();
    Bitmap bitmap;
    try {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
}

我已经尝试了不同的codeS rotateImage,1,2,3等,但没有code的工作。 rotateImage2()正在工作,但其严重影响质量,所以不能使用。

I have tried different codes rotateImage, 1, 2, 3 etc. But no code is working. rotateImage2() is working, but its badly affecting the quality, so can't use that.

我从这些code <一个href="http://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a-center-point">Android:如何旋转一个位图上的一个中心点并有网友说其工作,而不是我。 那么,什么错,我做的事情。

I got these code from Android: How to rotate a bitmap on a center point and there users say that its working, but not with me. So what wrong I'm doing.

推荐答案

坚持使用的 rotateImage2()的,使用更少的转换。这code对我的作品:

Stick with rotateImage2() and use less transformations. This code works for me:

private Bitmap rotateImage(String pathToImage) {

    // 1. figure out the amount of degrees
    int rotation = getImageRotation();

    // 2. rotate matrix by postconcatination
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation);

    // 3. create Bitmap from rotated matrix
    Bitmap sourceBitmap = BitmapFactory.decodeFile(pathToImage);
    return Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);        
}

要获取图像旋转<一个href="http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android">check你的其他职位。有关矩阵运算的更多的相关信息和他们之间的分歧,如setRotate和postRotate <一href="http://stackoverflow.com/questions/3855578/android-matrix-what-is-the-different-between-$p$pconcat-and-postconcat">check出这个帖子。

To get the image rotation check out your other post. For more infos on Matrix operations and their differences, e.g setRotate and postRotate check out this post.

这篇关于code旋转图像通过摄像头的意图不工作在Android的捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆