您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java 图片与byte数组互相转换

小志的博客 发布时间:2019-01-18 16:01:14 ,浏览量:0

1、图片到byte数组

     //图片到byte数组
    public byte[] image2byte(String path){
     byte[] data = null;
      FileImageInputStream input = null;
      try {
        input = new FileImageInputStream(new File(path));
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int numBytesRead = 0;
       while ((numBytesRead = input.read(buf)) != -1) {
       output.write(buf, 0, numBytesRead);
       }
       data = output.toByteArray();
       output.close();
       input.close();
     }
     catch (FileNotFoundException ex1) {
       ex1.printStackTrace();
     }
     catch (IOException ex1) {
       ex1.printStackTrace();
     }
     return data;
   }

2、byte数组到图片

      //byte数组到图片
    public void byte2image(byte[] data,String path){
      if(data.length            
关注
打赏
1661269038
查看更多评论
0.0406s