Java中如何将位图转换为字节数组呢?

陈欣 Java经验 发布时间:2023-08-08 10:50:49 阅读数:13924 1
下文笔者讲述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;
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202308/16914630767167.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者