導航:首頁 > 編程語言 > 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相關的資料

熱點內容
安卓軟體為什麼運行不了 瀏覽:519
什麼安卓手機是四方的 瀏覽:473
雲伺服器廠商租用多少錢 瀏覽:347
平安車貸解壓要多久 瀏覽:410
樂一php程序員待遇 瀏覽:162
ZDM命令無法識別 瀏覽:918
解壓小手工A4紙 瀏覽:977
鋼筋加密區是幾倍 瀏覽:666
編譯程序代碼軟體 瀏覽:802
怎麼恢復加密的東西 瀏覽:983
程序員賣茶 瀏覽:697
後端程序員英文 瀏覽:363
滴滴程序員平均月薪 瀏覽:591
如何使用ftp命令 瀏覽:787
小書亭下載的文件在哪手機文件夾 瀏覽:178
交叉編譯器編譯單個c文件 瀏覽:516
代理伺服器地址列表吧 瀏覽:934
java列出所有文件 瀏覽:870
壓縮包看圖軟體 瀏覽:193
sqlite在android中的應用 瀏覽:663