code to rotate image captured by camera intent not

2019-02-19 08:30发布

I have a problem that my image captured using camera intent gets rotated, which I asked at Why does an image captured using camera intent gets rotated on some devices on Android?

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

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.

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.

1条回答
对你真心纯属浪费
2楼-- · 2019-02-19 08:51

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

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.

查看更多
登录 后发表回答