如何在旋转图像时自动设置宽度和高度?

时间:2015-05-11 02:50:41

标签: android

当我以纵向模式拍摄照片时,它会被美化并且我使用此功能来旋转图像

private Bitmap rotateImage(String mCurrentPhotoPath, String rotationAngle){
    //Set hardCode size imageView 300dp
    int targetW = CommonUtils.ConvertDpTPpx(mContext, 300);
    int targetH = CommonUtils.ConvertDpTPpx(mContext, 300);
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    Matrix matrix = new Matrix();
    matrix.setRotate(Float.parseFloat(rotationAngle), (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    return Bitmap.createBitmap(bitmap, 0, 0, photoW/2, photoH/2, matrix, true);
}

当我在S4上构建设备时,它运行正确,但是当它在Sony Docomo(SO-04E)和LG LTE2上运行时,捕获照片和应用程序崩溃后。错误是x +宽度(图像)< bitmap.width。然后我编辑photoW / 4和photoH / 4所以索尼和LG运行正确但S4图像被裁剪。所以,在返回线上,我应该如何确定在所有设备上正确运行的宽度和高度。

0 个答案:

没有答案