缩小位图图像的大小后,图像被旋转



我的应用程序先拍照然后上传。由于拍摄的照片会很大,我必须缩小它的大小然后上传。因此,我使用以下代码来缩小相机拍摄的图像的大小,这些图像存储在sd卡中。但我不知道代码出了什么问题,如果是人像,图像会逆时针旋转90度,否则在横向模式下拍摄时图像会很好。

以下是我用来缩小大小的代码。

public File getReducedImage(File mediaFile){        
    Bitmap b = decodeFile(mediaFile);
    return getfileFromBitmap(b, mediaFile.getPath());
}
private File getfileFromBitmap(Bitmap b, String path) {     
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    //you can create a new file name "test.jpg" in sdcard folder.
    File f = new File(path);
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());          
        // remember close de FileOutput
        fo.close();
        Log.v(TAG, "returned");
        return f;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v(TAG, "Exception caught");
        return null;
    }
    //write the bytes in file
}
private Bitmap decodeFile(File f){
     final int IMAGE_MAX_SIZE=400;
    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();
        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / 
               (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }
        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
        Log.v(TAG, "error in bitmap conversion");
        e.printStackTrace();

    }
    return b;
}

编辑:事实上,问题是一旦调整图像的大小,它就会丢失方向信息。我通过适当的旋转解决了这个问题。以下是我的解决方案。希望它能帮助到别人。

公共文件getReducedImage(文件媒体文件){

    Bitmap b = decodeFileWithRotationIfNecessary(mediaFile);
    File f = getfileFromBitmap(b, mediaFile.getPath());     
    return f;
}

private File getfileFromBitmap(Bitmap b, String path) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    // you can create a new file name "test.jpg" in sdcard folder.
    File f = new File(path);
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        // remember close de FileOutput
        fo.close();
        return f;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v(TAG, "Exception caught");
        return null;
    }
    // write the bytes in file
}

private Bitmap decodeFileWithRotationIfNecessary(File f) {
    final int IMAGE_MAX_SIZE = 400;
    Bitmap b = null;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();
        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int) Math.pow(
                    2,
                    (int) Math.round(Math.log(IMAGE_MAX_SIZE
                            / (double) Math.max(o.outHeight, o.outWidth))
                            / Math.log(0.5)));
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
        Log.v(TAG, "error in bitmap conversion");
        e.printStackTrace();
    }
    Bitmap bMapRotate = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
            b.getHeight(), getMatrix(f), true);
    return bMapRotate;
}
private Matrix getMatrix(File f) {
    Matrix mat = new Matrix();
    mat.postRotate(90);
    try {
        ExifInterface exif = new ExifInterface(f.getPath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, -1);
        switch (orientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            Log.v(TAG, "flip horizontal");

            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            Log.v(TAG, "flip vertical");
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            Log.v(TAG, "rotate 180");
            mat.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            Log.v(TAG, "rotate 90");
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            Log.v(TAG, "rotate 270");
            mat.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            Log.v(TAG, "transpose");
            break;
        case ExifInterface.ORIENTATION_UNDEFINED:   
            Log.v(TAG, "undefined");
            break;
        case ExifInterface.ORIENTATION_NORMAL:
            Log.v(TAG, "normal");
            mat.postRotate(270);
            break;
        default:
            Log.v(TAG, "default");
        //  mat.postRotate(0);
            break;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v(TAG, "error in finding exif information");
    }
    return mat;
}

一个原因可能是您没有保存EXIF数据:

ExifInterface ei = new ExifInterface(<file-path>);
int o = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                           ExifInterface.ORIENTATION_NORMAL);
//--decode -- encode save--
ExifInterface ei2 = new ExifInterface(<new-file-path>);
ei2.setAttribute(ExifInterface.TAG_ORIENTATION,o);
ei2.saveAttributes();

默认相机应用程序将在应用正确方向的情况下保存。若您使用自己的代码来拍摄/修改图片,则必须以正确的方向保存图像。

最新更新