Java图片与byte数组如何互相转换

这期内容当中小编将会给大家带来有关Java图片与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;
 }
 //byte数组到图片
 public void byte2image(byte[] data,String path){
  if(data.length<3||path.equals("")) return;
  try{
  FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
  imageOutput.write(data, 0, data.length);
  imageOutput.close();
  System.out.println("Make Picture success,Please find image in " + path);
  } catch(Exception ex) {
   System.out.println("Exception: " + ex);
   ex.printStackTrace();
  }
 }
 //byte数组到16进制字符串
 public String byte2string(byte[] data){
  if(data==null||data.length<=1) return "0x";
  if(data.length>200000) return "0x";
  StringBuffer sb = new StringBuffer();
  int buf[] = new int[data.length];
  //byte数组转化成十进制
  for(int k=0;k

文件解析:

FileImageOutputStream 换成了 FileOutputStream
FileImageInputStream 换成 FileInputStream

上述就是小编为大家分享的Java图片与byte数组如何互相转换了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享文章:Java图片与byte数组如何互相转换
URL地址:http://csdahua.cn/article/gdescc.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流