Last active
January 14, 2024 16:21
-
-
Save spiritedRunning/51b62706f99f6dffab7a698de15d94bb to your computer and use it in GitHub Desktop.
bitmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 按分辨率压缩 | |
public Bitmap compressBitmap(Bitmap tmpBitmap) { | |
int w = tmpBitmap.getWidth() ; | |
int h = tmpBitmap.getHeight() ; | |
Matrix matrix = new Matrix(); | |
float scaleWidth = 800f / w; | |
float scaleHeight = 600f / h; | |
matrix.postScale(scaleWidth , scaleHeight); | |
Bitmap endBitmap = Bitmap. createBitmap( tmpBitmap, 0 , 0 , tmpBitmap.getWidth() , tmpBitmap.getHeight() , matrix, true ) ; | |
return endBitmap ; | |
} | |
// 调整bitmap格式为RGB_565, 并进行质量压缩 | |
public static Bitmap addInBitmapOptions(Bitmap srcBitmap, Bitmap bmp) { | |
LogUtil.d(TAG, "original Bitmap:" + bmp.getByteCount()); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
baos.reset(); | |
int beginRate = 100; | |
bmp.compress(Bitmap.CompressFormat.JPEG, beginRate, baos); | |
LogUtil.d(TAG, "file length: " + baos.toByteArray().length); | |
while (baos.toByteArray().length / 1024 > 50) { | |
baos.reset(); | |
beginRate -= 10; | |
bmp.compress(Bitmap.CompressFormat.JPEG, beginRate, baos); | |
if (beginRate == 60) { | |
break; | |
} | |
} | |
LogUtil.d(TAG, "compressed file length: " + baos.toByteArray().length); | |
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inMutable = true; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | |
options.inBitmap = srcBitmap; | |
} | |
options.inPreferredConfig = Config.RGB_565; | |
options.inPurgeable = true; | |
options.inInputShareable = true; | |
options.inJustDecodeBounds = false; | |
Bitmap endBitmap = BitmapFactory.decodeStream(bais, null, options); | |
LogUtil.d(TAG, "processed Bitmap: " + endBitmap.getByteCount()); | |
return endBitmap; | |
} | |
public void saveBitmap(Bitmap bitmap) { | |
// 首先保存图片 | |
File appDir = new File(Environment.getExternalStorageDirectory(), "asave_image"); | |
if (!appDir.exists()) { | |
appDir.mkdirs(); | |
} | |
String fileName = "temp_image" + System.currentTimeMillis() + ".png"; | |
File file = new File(appDir, fileName); | |
try { | |
FileOutputStream fos = new FileOutputStream(file); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); | |
fos.flush(); | |
fos.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private Bitmap byte2bitmap(byte[] bytes, int width, int height) { | |
Bitmap bitmap = null; | |
final int w = width; // 宽度 | |
final int h = height; | |
final YuvImage image = new YuvImage(bytes, ImageFormat.NV21, w, h, null); | |
ByteArrayOutputStream os = new ByteArrayOutputStream(bytes.length); | |
if (!image.compressToJpeg(new Rect(0, 0, w, h), 100, os)) { | |
return null; | |
} | |
byte[] tmp = os.toByteArray(); | |
bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length); | |
Matrix matrix = new Matrix(); | |
matrix.setRotate(0); | |
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); | |
return bitmap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment