import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
public class imgTest {
//图片到byte数组
public static byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
//byte数组到图片
public static void byte2image(byte[] data,String path){
if(data.length<3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
public static String byte2string(byte[] data){
if(data==null||data.length<=1) return "0x";
if(data.length>200000) return "0x";
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte数组转化成十进制
for(int k=0;k<data.length;k++){
buf[k] = data[k]<0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(int k=0;k<buf.length;k++){
if(buf[k]<16) sb.append("0"+Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return "0x"+sb.toString().toUpperCase();
}
public static void main(String[] args){
try {
InputStream in = new FileInputStream("D:\\图片\\11.jpg");
byte[] bytes = new byte[in.available()];
in.read(bytes);
byte2image(bytes,"D:\\dd.jpg");
// BufferedImage bufferedImage= ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
createImg(data, "C:\\Users\\yangcj\\Desktop\\image"); } public static void createImg(String data, String fileName) { try { File file = new File(fileName + ".jbig"); FileOutputStream fis...
包含了jpg转tif的三个jar包,同时包含了代码,有读取本地jpg转本地tif的代码,也有通过jpg的byte数组转tif的byte数组的,包你满意
Html2Image (JAVA)jar包public static byte[] generatePDF(StringReader strReader,String path) { try { FileOutputStream fos = new FileOutputStream(path); ByteArrayOutputStream os = new ...
网络文件下载程序-基于Java的源代码,给定一个文件的网络URL地址,本程序将从该地址获取(下载)文件,java环境的手机,可直接运行本程序,具体代码: in = (InputStream) Connector.openInputStream(url); //...
Java网络下载程序,从网上下载数据的例子,本例子展示的是下载一幅图像的数据,从创建网络对象到最后成功下载,具体编写了以下代码: while ((ch = in.read()) != -1){ //读取字节数 bos.write(ch); //写字节...
byte[] buf = new byte[4096]; int len = -1; while ((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { ...
因数据安全,需要将生成的报表xls,添加水印,所以自已在jxl中进行代码修改... byte imageData[] = new byte[(int)fileImg.length()]; FileInputStream fis = new FileInputStream(fileImg); fis.read(imageData); //
)PNG/BMP/WBMP/JPEG/JPG (java.awt.image.BufferedImage)MID/MIDI (javax.sound.midi.Sequence)WAV/AU/AIFF/SND (javax.sound....java.Jar)ZIP (org.magnos.asset.zip.Zip)GZ (org.magnos.asset.AssetInfo)DAT (byte...
byte imageData[] = new byte[(int)fileImg.length()]; FileInputStream fis = new FileInputStream(fileImg); fis.read(imageData); // must be 24 bit true-colour,bmp file // * @param imageByte //...
1.参数是图片路径:返回Byte[]类型: public byte[] GetPictureData(string imagepath) { /**/////根据图片文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);...
服务器在以下布局中发送 131 个字节(尽管现在是 1.8,这最多131 个,因为添加了压缩):Item Map ID (1byte) -> Scale -> (1byte) -> The x column we'重新编辑 (1byte) -> 地图数据 (128bytes) 使用这个,我们...
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader( process.getInputStream())); String freeSpace = "", line; while ((line = in.readLine()) != null) { if (line.length...
程序可以采用 JAVA 开发,但是因为它的虚拟机 (Virtual Machine) Dalvik ,是将 JAVA 的 bytecode 转成 自 己的格式,回避掉需要付给 SUN 有关 JAVA 的授权费用。 对手机制造者的影响 � Android 是款开源的移动计算...
byte a []=name.getBytes("ISO-8859-1"); name=new String(a); String sex = request.getParameter("sex"); String address = request.getParameter("address"); String tel = request.getParameter("tel"); ...