當前位置:首頁 » 編程語言 » java流圖片

java流圖片

發布時間: 2025-09-14 21:53:37

『壹』 java中如何把圖片轉換成二進制流

Java中將圖片轉為二進制流只需要使用FileImageInputStream取得圖片文件,然後使用ByteArrayOutputStream 寫入到二進制流中即可,下面是詳細代碼:


//圖片到byte數組
publicbyte[]image2byte(Stringpath){
byte[]data=null;
FileImageInputStreaminput=null;
try{
input=newFileImageInputStream(newFile(path));
ByteArrayOutputStreamoutput=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intnumBytesRead=0;
while((numBytesRead=input.read(buf))!=-1){
output.write(buf,0,numBytesRead);
}
data=output.toByteArray();
output.close();
input.close();
}
catch(FileNotFoundExceptionex1){
ex1.printStackTrace();
}
catch(IOExceptionex1){
ex1.printStackTrace();
}
returndata;
}

另外,如果需要將byte[]存回圖片或轉為String,則:

//byte數組到圖片
publicvoidbyte2image(byte[]data,Stringpath){
if(data.length<3||path.equals(""))return;
try{
=newFileImageOutputStream(newFile(path));
imageOutput.write(data,0,data.length);
imageOutput.close();
System.out.println("MakePicturesuccess,Pleasefindimagein"+path);
}catch(Exceptionex){
System.out.println("Exception:"+ex);
ex.printStackTrace();
}
}
//byte數組到16進制字元串
publicStringbyte2string(byte[]data){
if(data==null||data.length<=1)return"0x";
if(data.length>200000)return"0x";
StringBuffersb=newStringBuffer();
intbuf[]=newint[data.length];
//byte數組轉化成十進制
for(intk=0;k<data.length;k++){
buf[k]=data[k]<0?(data[k]+256):(data[k]);
}
//十進制轉化成十六進制
for(intk=0;k<buf.length;k++){
if(buf[k]<16)sb.append("0"+Integer.toHexString(buf[k]));
elsesb.append(Integer.toHexString(buf[k]));
}
return"0x"+sb.toString().toUpperCase();
}

『貳』 java中如何把一個圖片轉換成二進制流存入到類中啊

1.將Image圖像文件存入到資料庫

我們知道資料庫里的Image類型的數據是"二進制數據",因此必須將圖像文件轉換成位元組數組才能存入資料庫中.

要這里有關數據的操作略寫,我將一些代碼段寫成方法,方便直接調用.

//根據文件名(完全路徑)
public byte[] SetImageToByteArray(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
int streamLength = (int)fs.Length;
byte[] image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image;
}

//另外,在ASP.NET中通過FileUpload控制項得到的圖像文件可以通過以下方法
public byte[] SetImageToByteArray(FileUpload FileUpload1)
{
Stream stream = FileUpload1.PostedFile.InputStream;
byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
stream.Close();
return photo;
}

2.從SQL Server資料庫讀取Image類型的數據,並轉換成bytes[]或Image圖像文件

//要使用SqlDataReader要載入using System.Data.SqlClient命名空間
//將資料庫中的Image類型轉換成byte[]
public byte[] SetImage(SqlDataReader reader)
{
return (byte[])reader["Image"];//Image為資料庫中存放Image類型欄位
}

//將byte[]轉換成Image圖像類型
//載入以下命名空間using System.Drawing;/using System.IO;
using System.Data.SqlClient;*/
public Image SetByteToImage(byte[] mybyte)
{
Image image;
MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length);
image = Image.FromStream(mymemorystream);
return image;
}

『叄』 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();//關閉輸出流

}

熱點內容
什麼手游腳本好用 發布:2025-09-14 23:18:08 瀏覽:643
電腦主板和伺服器有什麼區別 發布:2025-09-14 23:16:33 瀏覽:112
android下載代碼 發布:2025-09-14 23:09:40 瀏覽:733
ftp拓展名 發布:2025-09-14 22:41:03 瀏覽:523
sql時間年月 發布:2025-09-14 22:40:00 瀏覽:808
web直播源碼 發布:2025-09-14 22:39:42 瀏覽:750
hibernate對象緩存 發布:2025-09-14 22:25:37 瀏覽:873
crsa加密文件 發布:2025-09-14 22:14:19 瀏覽:99
linuxc語言程序設計 發布:2025-09-14 22:09:09 瀏覽:842
android可逆加密 發布:2025-09-14 22:07:17 瀏覽:359