導航:首頁 > 編程語言 > java圖片string

java圖片string

發布時間:2023-10-17 17:07:20

1. 怎麼在java里添加背景圖片

可以新建個面板,在面板里放入帶圖片的JLabel,填滿面板即可。
JPanel jp = new JPanel(); //新建面板
jp.setLayout(new FlowLayout()); //設置面板布局
ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
this.setVisible(true);

2. 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圖片string相關的資料

熱點內容
人民幣怎麼演算法 瀏覽:754
什麼app可以聽懂刺蝟說話 瀏覽:596
安卓機內存小如何擴大 瀏覽:125
粉絲伺服器怎麼和安卓手機通信 瀏覽:398
初中數學競賽pdf 瀏覽:568
linux自定義安裝 瀏覽:188
fpic要在每個編譯文件 瀏覽:866
編譯原理廣義推導的定義 瀏覽:911
怎麼在已有的壓縮文件里加密碼 瀏覽:517
安卓手機怎麼設置系統軟體 瀏覽:766
php前端java後端 瀏覽:794
數據框轉換為矩陣python 瀏覽:74
單片機程序反匯編 瀏覽:853
編程和實物不一樣 瀏覽:880
天官賜福小說什麼app可看 瀏覽:208
原車空調改壓縮機 瀏覽:103
python調用其它文件中的函數 瀏覽:484
安卓車載大屏如何下載歌詞 瀏覽:959
刪除這些文件夾 瀏覽:675
新建文件夾怎麼設置快捷搜索 瀏覽:503