图片改二进制

import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.IOException
  
import javax.imageio.ImageIO
  
import sun.misc.BASE64Decoder
import sun.misc.BASE64Encoder
  
public class TestImageBinary {  
  static BASE64Encoder encoder = new sun.misc.BASE64Encoder()
  static BASE64Decoder decoder = new sun.misc.BASE64Decoder()
     
  public static void main(String[] args) {  
    System.out.println(getImageBinary())
       
    base64StringToImage(getImageBinary())
  }  
     
  static String getImageBinary(){  
    File f = new File("c://20090709442.jpg");     
    BufferedImage bi
    try {  
      bi = ImageIO.read(f)
      ByteArrayOutputStream baos = new ByteArrayOutputStream()
      ImageIO.write(bi, "jpg", baos)
      byte[] bytes = baos.toByteArray()
         
      return encoder.encodeBuffer(bytes).trim()
    } catch (IOException e) {  
      e.printStackTrace()
    }  
    return null
  }  
     
  static void base64StringToImage(String base64String){  
    try {  
      byte[] bytes1 = decoder.decodeBuffer(base64String)
         
      ByteArrayInputStream bais = new ByteArrayInputStream(bytes1)
      BufferedImage bi1 =ImageIO.read(bais)
      File w2 = new File("c://QQ.bmp");//可以是jpg,png,gif格式  
      ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动  
    } catch (IOException e) {  
      e.printStackTrace()
    }  
  }  
  
}

 

posted @   超级大菜鸡  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示