JAVA高级特性——二进制存储图片

import java.io.*;

/**
 * 将图片转为数组,输出成文件,再读取这个文件,获得这个数组,还原成图片
 * @author Administrator
 *
 *
 */
public class Text3 {
    public static void main(String[] args) {
        //获取图片aa.jpg,将图片信息保存到数组b中
        byte []b=Text3.imgArry("aa.jpg");
        //通过数组b写到文件bb.txt中去
        Text3.writeByteimg(b, "bb.txt");
        byte []c=Text3.imgin("bb.txt");
        Text3.writeimg(c, "cc.jpg");
            
        }
    
    
    
    /**
     * 用字节流获取图片,把字节数组用ByteArrayOutputStream 写到 bao里面去,,可以返回一个字节数组
     * @param path
     * @return
     */
    public static byte[] imgArry(String path){
        InputStream inImg=null;
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        try {
            inImg=new FileInputStream(path);
            byte [] b=new byte[1024];
            int len=-1;
            //将图片的所有字节通过数组b读取出来在写到bao里面去
            while((len=inImg.read(b))!=-1) {
                bao.write(b, 0, len);
            }
            //返回bao字节数组
            return bao.toByteArray();
            
        } catch (FileNotFoundException e) {            
            e.printStackTrace();
        } catch (IOException e) {    
            e.printStackTrace();
        }finally {
            try {
                bao.close();
                inImg.close();
            } catch (IOException e) {    
                e.printStackTrace();
            }
        }
        
        return null;

    }

    /**
     * 用二进制写出图片,保存到文件去
     * @param imgs
     * @param path
     */
    public static void writeByteimg(byte []imgs,String path) {
        DataOutputStream outimg=null;
        try {
            outimg=new DataOutputStream(new FileOutputStream(path));
            outimg.write(imgs);
            outimg.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outimg.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
        
        
    /**
     * 读取二进制保存的图片,放到一个字节数组中
     */

    public static byte[] imgin(String path) {
        DataInputStream imgin=null;
        try {
            imgin=new DataInputStream(new FileInputStream(path));
            //创建一个字节数组,数组长度等于图片返回的实际字节数
            byte[] b=new byte[imgin.available()];
            //读取图片信息放入b中,返回b
            imgin.read(b);
            return b;
        } catch (FileNotFoundException e) {        
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                imgin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 将图片输出
     */
    public static void writeimg(byte[]img,String path) {
        OutputStream ow=null;
        try {
            ow=new FileOutputStream(path);
            ow.write(img);
            ow.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                ow.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
                
    }
}

猜你喜欢

转载自www.cnblogs.com/EasyInsomnia/p/10198570.html