Java 调整大小后的图像大小大于原始图像
调整大小后,图像大小大于原始大小 原始图像细节Java 调整大小后的图像大小大于原始图像,java,android,image,bitmap,Java,Android,Image,Bitmap,调整大小后,图像大小大于原始大小 原始图像细节 尺寸1680x1050 大小553KB 调整大小的图像 尺寸720x1280 大小1.83 MB 我正在使用的方法 bitmap = Bitmap.createBitmap (bitmap, bitmapGapX, bitmapGapY, deviceWidth, deviceHeight); 有没有办法得到压缩后的图像。目前,iamge size比原始图像大得多有关详细信息,请参阅文档,尤其是第
- 尺寸
1680x1050
- 大小
553KB
- 尺寸
720x1280
- 大小
1.83 MB
bitmap = Bitmap.createBitmap (bitmap, bitmapGapX, bitmapGapY,
deviceWidth, deviceHeight);
有没有办法得到压缩后的图像。目前,iamge size比原始图像大得多有关详细信息,请参阅文档,尤其是第二个参数(质量):
质量压缩机提示,0-100。0表示小尺寸压缩,100表示最大质量压缩。某些格式,如无损PNG,将忽略质量设置
只需使用较低的质量设置即可获得较小的文件。如果传递给Bitmap.compress()的质量设置较大,并且原始文件是使用低质量设置保存的,则分辨率较低的已调整大小的图像很可能最终的文件大小大于原始图像
因此,请尝试使用较低的质量值,例如:
bitmap.compress(Bitmap.CompressFormat.JPEG, 75 /*quality setting*/, outputStream);
尝试不同的质量设置,直到找到质量和文件大小之间的良好平衡。我不知道如何调整图像大小,但您可以尝试使用此方法来减小大小。在后台线程中运行此方法
public String compressImage(String imageUri) {
Log.i(TAG, "qq imageUri = " + imageUri);
String filePath = getRealPathFromURI(imageUri);
Log.i(TAG, "qq filePath = " + filePath);
// getAxiomData( filePath);
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
float maxHeight = 816.0f;
float maxWidth = 612.0f;
float imgRatio = actualWidth / actualHeight;
float maxRatio = maxWidth / maxHeight;
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
options.inSampleSize = utils.calculateInSampleSize(options,
actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];
try {
bmp = BitmapFactory.decodeFile(filePath, options);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
try {
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight,
Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
float ratioX = actualWidth / (float) options.outWidth;
float ratioY = actualHeight / (float) options.outHeight;
float middleX = actualWidth / 2.0f;
float middleY = actualHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2,
middleY - bmp.getHeight() / 2, new Paint(
Paint.FILTER_BITMAP_FLAG));
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(),
matrix, true);
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fOut = null;
ByteArrayOutputStream uploadStream = new ByteArrayOutputStream();
String filename = getFilename();
try {
// Store in folder
fOut = new FileOutputStream(filename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
// Store in variable to upload
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
uploadStream);
byte[] byteArray = uploadStream.toByteArray();
strBase64Img[imgCount] = Base64.encodeToString(byteArray,
Base64.DEFAULT);
// Show in layout
finalBtmpImg[imgCount] = scaledBitmap;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return filename;
}
公共字符串压缩图像(字符串图像URI){
Log.i(标记“qq imageUri=“+imageUri”);
字符串filePath=getRealPathFromURI(imageUri);
Log.i(标记“qq filePath=“+filePath”);
//获取数据(文件路径);
位图缩放位图=空;
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
位图bmp=BitmapFactory.decodeFile(文件路径,选项);
int实际高度=options.outHeight;
int actualWidth=options.outWidth;
浮动最大高度=816.0f;
浮动最大宽度=612.0f;
浮动高度=实际宽度/实际高度;
浮点最大比值=最大宽度/最大高度;
如果(实际高度>最大高度| |实际宽度>最大宽度){
if(imgRatiomaxRatio){
imgRatio=最大宽度/实际宽度;
实际高度=(int)(imgRatio*实际高度);
实际宽度=(int)最大宽度;
}否则{
实际高度=(int)最大高度;
实际宽度=(int)最大宽度;
}
}
options.inSampleSize=utils.calculateInSampleSize(选项,
实际宽度、实际高度);
options.inJustDecodeBounds=false;
options.inDither=false;
options.inpurgable=true;
options.inInputShareable=true;
options.inTempStorage=新字节[16*1024];
试一试{
bmp=BitmapFactory.decodeFile(文件路径,选项);
}捕获(OutOfMemoryError异常){
异常。printStackTrace();
}
试一试{
scaledBimat=Bitmap.createBitmap(实际宽度、实际高度、,
位图.Config.ARGB_8888);
}捕获(OutOfMemoryError异常){
异常。printStackTrace();
}
浮动比率=实际宽度/(浮动)选项。向外宽度;
浮动比率=实际高度/(浮动)选项。超出高度;
浮动中间点x=实际宽度/2.0f;
浮动中间Y=实际高度/2.0f;
矩阵scaleMatrix=新矩阵();
scaleMatrix.setScale(ratioX、ratioY、middleX、middleY);
画布画布=新画布(缩放位图);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp,middleX-bmp.getWidth()/2,
middleY-bmp.getHeight()/2,新绘制(
Paint.FILTER_BITMAP_FLAG));
出口接口;
试一试{
exif=新的ExifInterface(文件路径);
int-orientation=exif.getAttributeInt(
ExiFinInterface.TAG_方向,0);
Log.d(“EXIF”,“EXIF:+方向”);
矩阵=新矩阵();
如果(方向==6){
矩阵旋转后(90);
Log.d(“EXIF”,“EXIF:+方向”);
}否则如果(方向==3){
矩阵旋转后(180);
Log.d(“EXIF”,“EXIF:+方向”);
}否则如果(方向==8){
矩阵旋转后(270);
Log.d(“EXIF”,“EXIF:+方向”);
}
scaledBitmap=Bitmap.createBitmap(scaledBitmap,0,0,
scaledBitmap.getWidth(),scaledBitmap.getHeight(),
矩阵,真);
}捕获(IOE异常){
e、 printStackTrace();
}
FileOutputStream fOut=null;
ByteArrayOutputStream uploadStream=新建ByteArrayOutputStream();
字符串filename=getFilename();
试一试{
//存储在文件夹中
fOut=新文件输出流(文件名);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG,100,fOut);
//存储在要上载的变量中
scaledBitmap.compress(Bitmap.CompressFormat.JPEG,100,
上传流);
byte[]byteArray=uploadStream.toByteArray();
strBase64Img[imgCount]=Base64.encodeToString(byteArray,
Base64(默认);
//在布局中显示
finalBtmpImg[imgCount]=缩放位图;
}catch(filenotfounde异常){
e、 printStackTrace();
}
返回文件名;
}
您是如何计算553 KB的?您是否使用Bitmap.compress()将已调整大小的图像保存为JPEG或PNG格式?您可以向下调整质量设置