导航:首页 > 编程语言 > 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相关的资料

热点内容
linuxsvn手册 浏览:264
程序员图鉴 浏览:536
东营程序员 浏览:714
发票上传参数没置服务器地址 浏览:43
程序员网上接单能挣多少钱 浏览:177
稀有传奇手游源码 浏览:549
u盘里的cd驱动加密是什么 浏览:418
遗传算法编码长度 浏览:978
pe装服务器需要注意什么 浏览:324
foreach计数php 浏览:529
php自连接 浏览:300
程序员被喷了怎么办 浏览:713
android消息数 浏览:265
为什么在服务器里输不了指令 浏览:33
程序员那么可爱前女友剧情介绍 浏览:106
centosjava环境变量配置 浏览:557
服务器主板被锁如何恢复 浏览:134
xpc语言编程软件 浏览:825
光遇安卓怎么解限制 浏览:304
元气骑士老版源码 浏览:108