當前位置:首頁 » 編程語言 » 圖片二進制轉換java

圖片二進制轉換java

發布時間: 2023-06-29 10:13:24

1. java如何把圖片轉換成二進制並存到oracle的blob中,求代碼

importjavax.imageio.ImageIO;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.IOException;


publicclassImageUtils{

publicstaticvoidmain(String[]args){
Stringstr=img2Binary("C:\Users\hny\Desktop\favicon.jpg");
System.out.println(str);
binary2Img("C:\Users\hny\Desktop\favicon2.jpg",str);
}

/**
*圖片轉二進制字元串
*
*@parampath圖片路徑
*@return
*/
publicstaticStringimg2Binary(Stringpath){
Filefile=newFile(path);
if(!file.exists()){
returnnull;
}

try{
BufferedImagebi=ImageIO.read(file);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
Stringsuffix=getSuffix(path);
ImageIO.write(bi,suffix,baos);
byte[]bytes=baos.toByteArray();

returnnewsun.misc.BASE64Encoder().encodeBuffer(bytes).trim();
}catch(IOExceptione){
e.printStackTrace();
}
returnnull;
}

/**
*字元串轉圖片文件
*
*@parampath圖片路徑
*@paramimgBinary圖片字元串
*/
publicstaticvoidbinary2Img(Stringpath,StringimgBinary){
try{
Filefile=newFile(path);

byte[]bytes1=newsun.misc.BASE64Decoder().decodeBuffer(imgBinary);
ByteArrayInputStreams=newByteArrayInputStream(bytes1);
BufferedImagebi1=ImageIO.read(s);
Stringsuffix=getSuffix(path);
ImageIO.write(bi1,suffix,file);
}catch(IOExceptione){
e.printStackTrace();
}
}

/**
*獲取圖片後綴名
*
*@parampath
*@return
*/
privatestaticStringgetSuffix(Stringpath){
intindex=path.contains(".")?path.lastIndexOf("."):-1;
if(index>-1){
returnpath.substring(index+1);
}
returnnull;
}
}

2. java把圖片轉換成二進制流

public static void main(String[] args) throws Exception {

File file = new File("d:\L.jpg");//圖片

FileInputStream fis = new FileInputStream(file);//把圖片變成流

FileOutputStream fos = new FileOutputStream(new File("E:\L.jpg")); //把圖片流寫入E盤

byte[] read = new byte[1024]; //每次讀取的位元組 可以自己定義 256 512 1024 2048 等。。。

int len = 0;

while((len = fis.read(read))!= -1){ //讀取變成流的圖片

fos.write(read,0,len);//寫入圖片

}

fis.close();//關閉輸入流

fos.close();//關閉輸出流

}

3. java 問題:怎樣把一個bin二進制圖片文件用java代碼打開求解!

Java中可以用java.awt.Toolkit類打開gif,jpg,png三種類型的二進制圖片文件,如果是其它類型的圖片,需要轉成上述格式的圖片才行。

我給你一個例子你看看吧。

importjava.awt.Frame;

importjava.awt.Graphics;

importjava.awt.Image;

importjava.awt.Toolkit;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

{

=1L;

Imageim;

//構造函數

publicLoadFromAppli(){

//根據地址裝入圖片

im=Toolkit.getDefaultToolkit().getImage("bg.png");//bg.png處寫你的圖片的絕對或相對路徑

//關閉窗口

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

});

}

//在Frame上顯示圖片

publicvoidpaint(Graphicsg){

g.drawImage(im,0,0,this);

}

publicstaticvoidmain(String[]args){

LoadFromApplif=newLoadFromAppli();

f.setSize(200,200);

f.setVisible(true);

}

}

4. Java里誰能給我一個將圖片轉換成二進制在將二進制轉換成圖片的demo!!!

importjava.awt.image.BufferedImage;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.IOException;

importjavax.imageio.ImageIO;

importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;

publicclassTestImageBinary{
staticBASE64Encoderencoder=newsun.misc.BASE64Encoder();
staticBASE64Decoderdecoder=newsun.misc.BASE64Decoder();

publicstaticvoidmain(String[]args){
System.out.println(getImageBinary());
base64StringToImage(getImageBinary());
}

staticStringgetImageBinary(){
Filef=newFile("c://a.jpg");
BufferedImagebi;
try{
bi=ImageIO.read(f);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
ImageIO.write(bi,"jpg",baos);
byte[]bytes=baos.toByteArray();

returnencoder.encodeBuffer(bytes).trim();
}catch(IOExceptione){
e.printStackTrace();
}
returnnull;
}

staticvoidbase64StringToImage(Stringbase64String){
try{
byte[]bytes1=decoder.decodeBuffer(base64String);

ByteArrayInputStreams=newByteArrayInputStream(bytes1);
BufferedImagebi1=ImageIO.read(s);
Filew2=newFile("c://index//a.bmp");//可以是jpg,png,gif格式
ImageIO.write(bi1,"jpg",w2);//不管輸出什麼格式圖片,此處不需改動
}catch(IOExceptione){
e.printStackTrace();
}
}

}

熱點內容
微軟不給源碼 發布:2025-02-11 16:13:37 瀏覽:38
php的get方法 發布:2025-02-11 16:12:30 瀏覽:967
源碼網嘉 發布:2025-02-11 16:07:06 瀏覽:192
免費ftp服務軟體 發布:2025-02-11 15:58:06 瀏覽:866
大櫻桃建園為什麼要配置授粉樹 發布:2025-02-11 15:58:00 瀏覽:629
五菱宏光s頂配有哪些配置 發布:2025-02-11 15:50:57 瀏覽:287
華為8加128配置有哪些 發布:2025-02-11 15:48:20 瀏覽:580
壓縮機三轉子 發布:2025-02-11 15:45:54 瀏覽:828
linux操作系統shell 發布:2025-02-11 15:45:53 瀏覽:339
安卓模擬器如何選擇安裝 發布:2025-02-11 15:34:26 瀏覽:177