Java中如何将位图转换为字节数组呢?
下文笔者讲述java中位图转换为字节数组的方法及示例分享,如下所示
Java中图片转字节数组的实现思路
步骤1: 图片读取到字节数组中 步骤2: 图片--->程序: FileInputStream 步骤3: 程序--->字节数组: ByteArrayOutputStream例:图片转换为字节数组
public static byte[] fileToByteArray(String filePath) { File file = new File(filePath); byte[] ds = null; //选择流 InputStream zp = null; ByteArrayOutputStream boos = null; boos = new ByteArrayOutputStream(); try { zp = new FileInputStream(file); byte[] frush = new byte[1024];//1024表示1k为一段 int len = -1; while((len=zp.read(frush))!=-1) { boos.write(frush,0,len);//写出到字节数组中 } boos.flush(); return boos.toByteArray(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(zp!=null) { try { zp.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。