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

熱點內容
一個普通程序員能幹多久 瀏覽:934
adobe的PDF轉換器 瀏覽:853
單片機數字碼表匯編 瀏覽:477
單片機開發用的軟體 瀏覽:151
程序員8個演算法題 瀏覽:401
php題庫系統 瀏覽:221
王牌戰爭文明重啟選什麼伺服器 瀏覽:653
簡述對稱加密法 瀏覽:658
c語言數學編程 瀏覽:990
F1B命令 瀏覽:630
cs命令快捷鍵 瀏覽:852
阿里雲購買伺服器如何用現金支付 瀏覽:691
pythontime等待 瀏覽:988
單片機串列通信方式 瀏覽:498
android表格demo 瀏覽:278
安卓怎麼讓相冊不顯示網頁 瀏覽:314
php文件系統源碼 瀏覽:727
易信java 瀏覽:501
北侖二級壓縮螺桿機 瀏覽:63
加密路線免費入口 瀏覽:759