gpt4 book ai didi

android - 缩小位图图像后图像旋转

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:57 25 4
gpt4 key购买 nike

我的应用程序拍照然后上传。由于拍摄的照片尺寸较大,我必须缩小尺寸然后上传。因此,我使用以下代码来减小存储在 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;
}

编辑:实际上,问题是图像在调整大小时丢失了方向信息。我通过适当旋转解决了它。以下是我的解决方案。希望它能对某人有所帮助。

公共(public)文件 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();

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

关于android - 缩小位图图像后图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14701584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com