gpt4 book ai didi

java - 在 Android 中从位图保存图像时如何设置 dpi 图像?

转载 作者:行者123 更新时间:2023-11-29 02:36:39 38 4
gpt4 key购买 nike

我使用这个函数将位图保存到 sdcard 上的文件中:

private static File storeImage(Context context, Bitmap image) {
File pictureFile = getOutputMediaFile(context);
if (pictureFile == null) {
return null;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return pictureFile;
}

private static File getOutputMediaFile(Context context){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ context.getPackageName()
+ "/Files");

// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
File mediaFile;
String mImageName="IMG_"+ timeStamp +".png";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}

当我打开文件并查看图像 DPI 信息时,它显示 72 像素/英寸 like this

如何将其设置为 300 像素/英寸或其他值?

最佳答案

这些是我在保存时将dpi设置为位图的方法,引用here还有here .

public void storeImage(Bitmap image) {
try {
File pictureFile = new File("yourpath");

FileOutputStream fos = new FileOutputStream(pictureFile);


ByteArrayOutputStream imageByteArray = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, imageByteArray);
byte[] imageData = imageByteArray.toByteArray();

//300 will be the dpi of the bitmap
setDpi(imageData, 300);

fos.write(imageData);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}


public void setDpi(byte[] imageData, int dpi) {
imageData[13] = 1;
imageData[14] = (byte) (dpi >> 8);
imageData[15] = (byte) (dpi & 0xff);
imageData[16] = (byte) (dpi >> 8);
imageData[17] = (byte) (dpi & 0xff);
}

关于java - 在 Android 中从位图保存图像时如何设置 dpi 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577226/

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