Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android位图从画布或BitmapFactory.createBitmap(src、x、y、w、h)绘制错误_Android_Image_Canvas_Bitmap - Fatal编程技术网

Android位图从画布或BitmapFactory.createBitmap(src、x、y、w、h)绘制错误

Android位图从画布或BitmapFactory.createBitmap(src、x、y、w、h)绘制错误,android,image,canvas,bitmap,Android,Image,Canvas,Bitmap,在Android上,我试图将从相机拍摄的图像位图裁剪为比原始照片更小的固定宽度/宽度,但是当我创建裁剪图像时,结果总是会旋转(即使没有定义矩阵),请参见 与原始图像和剪切图像一样,正在旋转且显示不正确 为什么createBitmap方法会旋转要在目标位图中绘制的位图 相关代码如下 try { int dstWidth = params[0].intValue(); int dstHeight = params[1].intValue();

在Android上,我试图将从相机拍摄的图像位图裁剪为比原始照片更小的固定宽度/宽度,但是当我创建裁剪图像时,结果总是会旋转(即使没有定义矩阵),请参见 与原始图像和剪切图像一样,正在旋转且显示不正确

为什么createBitmap方法会旋转要在目标位图中绘制的位图

相关代码如下

try {       int dstWidth = params[0].intValue();
            int dstHeight = params[1].intValue();
            int targetSize = Math.min(dstWidth, dstHeight);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(mPhotoUri), null, options); 
//calculate min inSampleSize for avoiding memory problems
            options.inSampleSize = calculateImageInSampleSize(options, targetSize, targetSize);
            options.inJustDecodeBounds = false;
            Bitmap originalPhotoBitmap = BitmapFactory.decodeStream(getActivity().getContentResolver()
                    .openInputStream(mPhotoUri), null, options);
            mOriginalBitmap = Bitmap.createBitmap(originalPhotoBitmap, originalPhotoBitmap.getWidth() - targetSize,
                                                        originalPhotoBitmap.getHeight() - targetSize, targetSize, targetSize);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//codefor inSampleSize calculation from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

private int calculateImageInSampleSize(BitmapFactory.Options originalSize, int targetWidth, int targetHeight) {
    final int width = originalSize.outWidth;
    final int height = originalSize.outHeight;
    int inSampleSize = 1;

    if (height > targetHeight || width > targetWidth) {
        final int halfHeight = height /2;
        final int halfWidth = width /2;

        while ((halfHeight /inSampleSize) > targetHeight &&
                (halfWidth / inSampleSize) > targetWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

每个设备在相机中都遵循不同的方向,所以您可以按照此示例解决您的问题

private Bitmap imageRotating(String filePath){
    try{
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        return rotateBitmap(decodeSampledBitmapFromResource(filePath,150,224), orientation);
    }
    catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    try{
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

检查这个我不知道为什么,但是ExiForeOrientation总是返回0未定义,所以没有应用旋转,通过使用BitmapFactory.Options.inJustDecodeOptions进行解码,我得到了一个更简单的解决方案来检查outWidth>outHeight,然后是横向和所需旋转,更简单的方法,但有效,总之@Thirmoorthy Moorthy的回答为我指明了正确的方向,所以我接受你的回答