BufferedImage

一、BufferedImage的介绍

Image(其全路径为java.awt.Image)是一个抽象类,BufferedImage是其实现类,是一个带缓冲区的图像类。BufferedImage主要作用是将一幅图片加载到内存中(BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便地操作这个图片),提供获得绘图对象、图像缩放、选择图像平滑度等功能,通常用来做图片大小变换、图片变灰、设置透明不透明等。

二、为什么要将byte数组转为BufferedImage

在数据传输中,图片是不能直接传的,因此需要把图片转为字节数组,然后再进行数据传输就比较方便。之所以将图片文件转为BufferedImage,是因为BufferedImage可以操作图像,可以使用下述两种方法获取到BufferedImage。

方法一:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class Demo {

    public static void main(String[] args) throws Exception {
        String filePath = "D:\\test\\测试文件.png";
        BufferedImage bufferedImage = ImageIO.read(new File(filePath));
    }
}

方法二

byte[] imgByte = "图片数据".getBytes();
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imgByte));

三、BufferedImage转为byte数组

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

public class Demo {

    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
        BufferedImage bufferedImage = ImageIO.read(new File("D:\\test\\测试文件.png"));
        ImageIO.write(bufferedImage,"png",byteArrayOutputStream);
        byte[] imgBytes = byteArrayOutputStream.toByteArray();
    }
}

四、BufferedImage转为File文件

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        String filePath = "D:\\test\\目标文件.png";
        FileOutputStream fileOutputStream =new FileOutputStream(filePath);
        BufferedImage bufferedImage = ImageIO.read(new File("D:\\test\\测试文件.png"));
        ImageIO.write(bufferedImage,"png",fileOutputStream);
    }
}

五、编写一个网络程序,通过Socket将图片从服务器端传到客户端,并存入文件系统 

服务端:

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;

import javax.imageio.ImageIO;

public class Server {

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(8889);
        System.out.println("服务器开启连接...端口为8889");
        Socket socket = server.accept();
        while(true){
            System.out.println("一客户端连接服务器,服务器传输图片...");
            //读取文件并传输
            BufferedImage image = ImageIO.read(new File("D:\\test\\测试文件.png"));	
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "png", out);
            byte[] b = out.toByteArray();
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.write(b);
            socket.close();
            System.out.println("图片传送完毕,服务器开启连接...端口为8889");
            socket = server.accept();
        }
    }
}

客户端:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.net.Socket;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ClientFrame extends JFrame {

    int count = 1;
    BufferedImage image;

    public ClientFrame() {
        // 1.设置Frame(窗口)界面的大小
        setSize(595, 842);
        // 2.创建“获取图像”的按钮
        JButton button = new JButton("获取图像");
        // 3.设置按钮的位置
        add(button, BorderLayout.NORTH);
        // 4.按钮添加监听
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                try {
                    // 5.创建连接,请求数据
                    Socket socket = new Socket("localhost", 8889);
                    PrintWriter out = new PrintWriter(socket.getOutputStream());
                    out.print("请求数据");
                    // 6.接收数据
                    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                    byte[] b = new byte[1024];
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int length = 0;
                    while ((length = dataInputStream.read(b)) != -1) {
                        bos.write(b, 0, length);
                    }
                    image = ImageIO.read(new ByteArrayInputStream(bos.toByteArray()));
                    // 7.将图像绘制到Pannel面板
                    repaint();
                    // 8.将获取到的文件保存到执行的路径下
                    ImageIO.write(image, "png", new File("D:\\test\\output-" + count + ".png"));
                    System.out.println("第"+count+"次请求");
                    count++;
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        JPanel panel = new JPanel();
        add(panel);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(image, 0, 20, 595, 842, null);//image为BufferedImage类型
    }

    public static void main(String[] args) {
        ClientFrame frame = new ClientFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

你可能感兴趣的:(文件处理,java)