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 使用矩阵旋转图像在保存时会扭曲图像_Android_Image_Bitmap_Camera_Jpeg - Fatal编程技术网

Android 使用矩阵旋转图像在保存时会扭曲图像

Android 使用矩阵旋转图像在保存时会扭曲图像,android,image,bitmap,camera,jpeg,Android,Image,Bitmap,Camera,Jpeg,我正在安卓系统中实现一个定制摄像头。我使用的其中一款手机的默认显示方向是横向模式。拍照时图像的预览效果良好。尽管如此,当图像旋转并保存时,图像会失真 这是我用来旋转图像的代码: Display display = ((WindowManager) MyCamera.this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = display.getRotation();

我正在安卓系统中实现一个定制摄像头。我使用的其中一款手机的默认显示方向是横向模式。拍照时图像的预览效果良好。尽管如此,当图像旋转并保存时,图像会失真

这是我用来旋转图像的代码:

    Display display = ((WindowManager) MyCamera.this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    int rotation = display.getRotation();

    if (rotation == Surface.ROTATION_0) {
        rotation = 90;

    } else if (rotation == Surface.ROTATION_90) {
        rotation = 180;

    } else if (rotation == Surface.ROTATION_180) {
        rotation = 270;

    } else if (rotation == Surface.ROTATION_270) {
        rotation = 360;
    } 

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inScaled = false;
    options.inDensity=metrics.densityDpi;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    bm = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length, options);

    Matrix m = new Matrix();
    m.postRotate(rotation);                 

    Bitmap nbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);

    file = MyCamera.this.saveBitmap(nbm);
这是我用来保存位图图像的代码:

private File saveBitmap(Bitmap image) {

    File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

    if (pictureFile == null) {
        Log.d(TAG,"Error creating media file, check storage permissions: ");// e.getMessage());
        return null;
    } 

    try {



        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();

    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
        finish();
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
        finish();
    }  

    return pictureFile;

}
以下是一些示例,介绍了图像在相机预览模式下的外观以及保存在手机外部存储器中后的外观


相机预览是在拍照后进行的。保存的照片截图是如何将照片存储在手机上的。我无法说出是什么被扭曲了,因为这看起来像两幅不同的图像。上图左上角有一盏灯,在下图中应该是可见的,但不是。它们是两个不同的图像。你是对的,但这是一直发生的事情。由于某种原因,我上传了不同的,我的错误。但是,您可以清楚地看到第二个图像被扭曲了。