是否可以将 bytearray/bitmap 压缩到特定大小?
Is it possible to zip a bytearray/bitmap to a specific size?
我试图将图像保存到 SQLite 数据库中,但即使我压缩并重新缩放位图,文件仍然太大,尤其是当您从 phone 的相机捕获图像时。
有没有办法将 bytearray/bitmap 压缩成特定大小,比如从 4mb 到 500kb?如果是,我该怎么做?
private void uriToFile(Uri uri, File sdImageMainDirectory) throws IOException {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
OutputStream stream = new FileOutputStream(sdImageMainDirectory);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
int maxSize = 10;
float bitmapRatio = (float) bitmapWidth / (float) bitmapHeight;
if(bitmapRatio > 0){
bitmapWidth = maxSize;
bitmapHeight = (int) (bitmapWidth / bitmapRatio);
}
else{
bitmapHeight = maxSize;
bitmapWidth = (int) (bitmapHeight * bitmapRatio);
}
bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
stream.flush();
stream.close();
}
您实际上并不是在压缩缩放后的图像,而是在压缩原始图像。将最后四行更改为:
Bitmap scaled = bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
scaled.compress(Bitmap.CompressFormat.JPEG, 30, stream);
stream.flush();
stream.close();
createScaledBitmap()
returns 缩放后的位图,你只是失去了结果。
您应该能够将 maxSize
设置为 1000 并查看小于 500k 的文件大小。
您也可以使用这些代码
String[] filePathColumn = {MediaStore.Images.Media.DATA};
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();
**Bitmap bitmapImage = BitmapFactory.decodeFile(img_Decodable_Str);
Bitmap converetdImage = getResizedBitmap(bitmapImage, 500);**
我试图将图像保存到 SQLite 数据库中,但即使我压缩并重新缩放位图,文件仍然太大,尤其是当您从 phone 的相机捕获图像时。
有没有办法将 bytearray/bitmap 压缩成特定大小,比如从 4mb 到 500kb?如果是,我该怎么做?
private void uriToFile(Uri uri, File sdImageMainDirectory) throws IOException {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
OutputStream stream = new FileOutputStream(sdImageMainDirectory);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
int maxSize = 10;
float bitmapRatio = (float) bitmapWidth / (float) bitmapHeight;
if(bitmapRatio > 0){
bitmapWidth = maxSize;
bitmapHeight = (int) (bitmapWidth / bitmapRatio);
}
else{
bitmapHeight = maxSize;
bitmapWidth = (int) (bitmapHeight * bitmapRatio);
}
bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
stream.flush();
stream.close();
}
您实际上并不是在压缩缩放后的图像,而是在压缩原始图像。将最后四行更改为:
Bitmap scaled = bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
scaled.compress(Bitmap.CompressFormat.JPEG, 30, stream);
stream.flush();
stream.close();
createScaledBitmap()
returns 缩放后的位图,你只是失去了结果。
您应该能够将 maxSize
设置为 1000 并查看小于 500k 的文件大小。
您也可以使用这些代码
String[] filePathColumn = {MediaStore.Images.Media.DATA};
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();
**Bitmap bitmapImage = BitmapFactory.decodeFile(img_Decodable_Str);
Bitmap converetdImage = getResizedBitmap(bitmapImage, 500);**