当前位置: 移动技术网 > IT编程>开发语言>Java > Java 将图片转二进制再将二进制转成图片

Java 将图片转二进制再将二进制转成图片

2019年10月09日  | 移动技术网IT编程  | 我要评论
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("f://123456.jpg");    //这里gif动态图不可以,虽然在后面也能输出gif格式,但是却不是动图
        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("f://meinv.bmp");//可以是jpg,png格式   
            imageio.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动   
        } catch (ioexception e) {   
            e.printstacktrace();   
        }   
    }   
  
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网