Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換
更新時(shí)間:2018年03月31日 10:28:05 作者:我心永恒
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換,將圖片轉(zhuǎn)二進(jìn)制再將二進(jìn)制轉(zhuǎn)成圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Java將圖片轉(zhuǎn)二進(jìn)制再將二進(jìn)制轉(zhuǎn)成圖片,供大家參考,具體內(nèi)容如下
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);//不管輸出什么格式圖片,此處不需改動(dòng)
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Excel文件加密保護(hù)數(shù)據(jù)安全
這篇文章主要為大家介紹了Java Excel文件加密保護(hù)數(shù)據(jù)安全的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
五種單件模式之Singleton的實(shí)現(xiàn)方法詳解
本篇文章是對(duì)Singleton的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Java Springboot 重要知識(shí)點(diǎn)整理匯總
Spring Boot作為微服務(wù)中最好的Java框架,本文主要為大家整理匯總了七個(gè)Spring Boot的重要知識(shí)點(diǎn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11
java圖片和文本同時(shí)提交到表單的實(shí)例代碼
在本篇文章里小編給大家整理的是關(guān)于java實(shí)現(xiàn)圖片和文本同時(shí)提交到表單的相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-02-02
java得到某年某周的第一天實(shí)現(xiàn)思路及代碼
某年某周的第一天,此功能如何使用java編程得到呢?既然有了問題就有解決方法,感興趣的朋友可以了解下本文,或許會(huì)給你帶來意想不到的收獲哦2013-01-01

