gpt4 book ai didi

android - 如何在不破坏Android质量的情况下减小图像尺寸

转载 作者:行者123 更新时间:2023-11-29 01:28:31 25 4
gpt4 key购买 nike

                Bitmap bmp = getResizedBitmap(
BitmapFactory.decodeByteArray(data, 0, data.length),
500);


public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();

float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}

这是我的代码使用 getResizedBitmap 我可以减小图像尺寸但无法在 android 中保持其原始质量请告诉我如何保持质量良好以便图像尺寸减小并且质量不差请给我建议!

最佳答案

您好,请尝试下面的代码,希望它能满足您的需求

public static Bitmap scaleImage(String p_path, int p_reqHeight, int p_reqWidth) throws Throwable
{
Bitmap m_bitMap = null;
System.gc();
File m_file = new File(p_path);
if (m_file.exists())
{
BitmapFactory.Options m_bitMapFactoryOptions = new BitmapFactory.Options();
m_bitMapFactoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
m_bitMapFactoryOptions.inSampleSize = calculateInSampleSize(m_bitMapFactoryOptions, p_reqHeight, p_reqWidth);
m_bitMapFactoryOptions.inJustDecodeBounds = false;
m_bitMap = BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
}
else
{
throw new Throwable(p_path + " not found or not a valid image");
}
return m_bitMap;
}


// Helper method
private static int calculateInSampleSize(BitmapFactory.Options p_options, int p_reqWidth, int p_reqHeight)
{
// Raw height and width of image
final int m_height = p_options.outHeight;
final int m_width = p_options.outWidth;
int m_inSampleSize = 1;
if (m_height > p_reqHeight || m_width > p_reqWidth)
{
final int m_halfHeight = m_height / 2;
final int m_halfWidth = m_width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((m_halfHeight / m_inSampleSize) > p_reqHeight && (m_halfWidth / m_inSampleSize) > p_reqWidth)
{
m_inSampleSize *= 2;
}
}
return m_inSampleSize;
}

关于android - 如何在不破坏Android质量的情况下减小图像尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563543/

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