Android - 安卓中Bitmap对象和字节流之间的相互转换(转)
转载自:代码很实用所以转载过来,又因为转载的也是别人转载的文章,所以出处不知道,请见谅!
android 将图片内容解析成字节数组;将字节数组转换为ImageView可调用的Bitmap对象;图片缩放;把字节数组保存为一个文件;把Bitmap转Byte
1 import java.io.BufferedOutputStream; 2 import java.io.ByteArrayOutputStream; 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 import android.graphics.Bitmap; 9 import android.graphics.BitmapFactory; 10 import android.graphics.Matrix; 11 12 public class ImageDispose { 13 14 /** 15 * @param 将图片内容解析成字节数组 16 * @param inStream 17 * @return byte[] 18 * @throws Exception 19 */ 20 public static byte[] readStream(InputStream inStream) throws Exception { 21 byte[] buffer = new byte[1024]; 22 int len = -1; 23 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 24 while ((len = inStream.read(buffer)) != -1) { 25 outStream.write(buffer, 0, len); 26 } 27 byte[] data = outStream.toByteArray(); 28 outStream.close(); 29 inStream.close(); 30 return data; 31 } 32 33 /** 34 * @param 将字节数组转换为ImageView可调用的Bitmap对象 35 * @param bytes 36 * @param opts 37 * @return Bitmap 38 */ 39 public static Bitmap getPicFromBytes(byte[] bytes, 40 BitmapFactory.Options opts) { 41 if (bytes != null) 42 if (opts != null) 43 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, 44 opts); 45 else 46 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 47 return null; 48 } 49 50 /** 51 * @param 图片缩放 52 * @param bitmap 对象 53 * @param w 要缩放的宽度 54 * @param h 要缩放的高度 55 * @return newBmp 新 Bitmap对象 56 */ 57 public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){ 58 int width = bitmap.getWidth(); 59 int height = bitmap.getHeight(); 60 Matrix matrix = new Matrix(); 61 float scaleWidth = ((float) w / width); 62 float scaleHeight = ((float) h / height); 63 matrix.postScale(scaleWidth, scaleHeight); 64 Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, 65 matrix, true); 66 return newBmp; 67 } 68 69 /** 70 * 把Bitmap转Byte 71 */ 72 public static byte[] Bitmap2Bytes(Bitmap bm){ 73 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 74 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 75 return baos.toByteArray(); 76 } 77 78 /** 79 * 把字节数组保存为一个文件 80 */ 81 public static File getFileFromBytes(byte[] b, String outputFile) { 82 BufferedOutputStream stream = null; 83 File file = null; 84 try { 85 file = new File(outputFile); 86 FileOutputStream fstream = new FileOutputStream(file); 87 stream = new BufferedOutputStream(fstream); 88 stream.write(b); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } finally { 92 if (stream != null) { 93 try { 94 stream.close(); 95 } catch (IOException e1) { 96 e1.printStackTrace(); 97 } 98 } 99 } 100 return file; 101 } 102 }
博客园文作者:Citrusliu
博文地址:https://www.cnblogs.com/citrus
分类:
Java/Android
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性