當前位置:首頁 » 編程語言 » 圖片二進制轉換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-07-17 11:32:27 瀏覽:519
伺服器系統盤一般做什麼陣列 發布:2025-07-17 11:16:05 瀏覽:362
如何用c語音實現放射密碼 發布:2025-07-17 11:13:03 瀏覽:734
lol腳本2017 發布:2025-07-17 10:52:18 瀏覽:826
編譯條件有哪些 發布:2025-07-17 10:42:51 瀏覽:333
apache設置靜態內容緩存時間 發布:2025-07-17 10:32:32 瀏覽:294
蘋果6不能緩存的視頻 發布:2025-07-17 10:31:44 瀏覽:404
如何把文件夾共享 發布:2025-07-17 10:23:49 瀏覽:298
linux查看命令大全 發布:2025-07-17 10:21:24 瀏覽:976
安卓應用程序和蘋果手機哪個好 發布:2025-07-17 10:20:43 瀏覽:411