`
liumayulingyan
  • 浏览: 148521 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

java img byte互转

    博客分类:
  • java
阅读更多
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();
        }
    }
}

分享到:
评论

相关推荐

    java jar包jbig转bmp

    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包.7z

    包含了jpg转tif的三个jar包,同时包含了代码,有读取本地jpg转本地tif的代码,也有通过jpg的byte数组转tif的byte数组的,包你满意

    Html2Image (JAVA)jar包

    Html2Image (JAVA)jar包public static byte[] generatePDF(StringReader strReader,String path) { try { FileOutputStream fos = new FileOutputStream(path); ByteArrayOutputStream os = new ...

    网络文件下载程序-基于Java源代码.rar

    网络文件下载程序-基于Java的源代码,给定一个文件的网络URL地址,本程序将从该地址获取(下载)文件,java环境的手机,可直接运行本程序,具体代码:  in = (InputStream) Connector.openInputStream(url); //...

    Java网络下载(下载一张图片文件)程序.rar

    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 { ...

    java jxl添加水印(基于jxl2.6.12修改编译版)_之前发布的大家不要去下,那个版本有问题,本版本经过测试

    因数据安全,需要将生成的报表xls,添加水印,所以自已在jxl中进行代码修改... byte imageData[] = new byte[(int)fileImg.length()]; FileInputStream fis = new FileInputStream(fileImg); fis.read(imageData); //

    Java资源加载库Azzet.zip

    )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...

    java jxl添加水印(基于jxl2.6.12修改)(因编译打包问题正在修正,请勿下载)

    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);...

    ImgMap-BufferOverflow:新的 LosingPixels 存储库。 这个存储库提供了 ImgMap 的最新重写

    服务器在以下布局中发送 131 个字节(尽管现在是 1.8,这最多131 个,因为添加了压缩):Item Map ID (1byte) -&gt; Scale -&gt; (1byte) -&gt; The x column we'重新编辑 (1byte) -&gt; 地图数据 (128bytes) 使用这个,我们...

    jsp探针 ver0.1

    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...

    新版Android开发教程.rar

    程序可以采用 JAVA 开发,但是因为它的虚拟机 (Virtual Machine) Dalvik ,是将 JAVA 的 bytecode 转成 自 己的格式,回避掉需要付给 SUN 有关 JAVA 的授权费用。 对手机制造者的影响 � Android 是款开源的移动计算...

    《Web应用技术》实验

    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"); ...

Global site tag (gtag.js) - Google Analytics