转载地址:http://blog.163.com/yuhua_kui/blog/static/9679964420139118203818/
1. 创建一个BufferedImage对象
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
实现代码:
public class Test extends Frame{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int width = 100; int height = 100; // 1.创建一个不带透明色的BufferedImage对象 BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 2.创建一个带透明色的BufferedImage对象 bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // 3.创建一个与屏幕相适应的BufferedImage对象 GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); // Create an image that does not support transparency bimage = gc.createCompatibleImage(width, height, Transparency.OPAQUE); // Create an image that supports transparent pixels bimage = gc.createCompatibleImage(width, height, Transparency.BITMASK); // Create an image that supports arbitrary levels of transparency bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT); } // 4.当然我们也可以在图形上下文来创建一个BufferedImage对象 public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; int width = 100; int height = 100; // Create an image that does not support transparency BufferedImage bimage = g2d.getDeviceConfiguration() .createCompatibleImage(width, height, Transparency.OPAQUE); // Create an image that supports transparent pixels bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.BITMASK); // Create an image that supports arbitrary levels of transparency bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); } } 2.使用BufferedImage的图像剪裁: public static void main(String[] args) { try { //从特定文件载入 BufferedImage bi = ImageIO.read(new File("c:\\test.jpg")); bi.getSubimage(0, 0, 10, 10); } catch (IOException e) { e.printStackTrace(); } }
注意ImageIO.read进行读图操作与以往的InputStream读取比较方便!
3.BufferedImage类型与Byte[]流之间相互转化,以及BufferedImage的显示与传输
一、需要用到的类
java.awt.image.BufferedImage;
javax.imageio.ImageIO;
java.io.*;
二、为什么要将BufferedImage转为byte数组
在传输中,图片是不能直接传的,因此需要把图片变为字节数组,然后传输比较方便;只需要一般输出流的write方法即可;而字节数组变成BufferedImage能够还原图像;
三、如何取得BufferedImage
BufferedImage image = ImageIO.read(new File("1.gif"));
四、BufferedImage ---->byte[]
ImageIO.write(BufferedImage image,String format,OutputStream out);方法可以很好的解决问题;
参数image表示获得的BufferedImage;
参数format表示图片的格式,比如“gif”等;
参数out表示输出流,如果要转成Byte数组,则输出流为ByteArrayOutputStream即可;
执行完后,只需要toByteArray()就能得到byte[];
五、byte[] ------>BufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(byte[]b); //将b作为输入流; BufferedImage image = ImageIO.read(InputStream in); //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
六、显示BufferedImage
public void paint(Graphics g){
super.paint(g);
g.drawImage(image); //image为BufferedImage类型
}
七、实例
要求:编写一个网络程序,通过Socket将图片从服务器端传到客户端,并存入文件系统;
Server端:
package org.exam3;
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 T6Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888);
Socket s = server.accept();
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedImage image = ImageIO.read(new File("1.gif"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(image, "gif", out);
byte[] b = out.toByteArray();
dout.write(b);
s.close();
}
}
Client端:
package org.exam3;
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.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 T6Client extends JFrame {
JButton button;
MyPanel panel;
public T6Client() {
setSize(300, 400);
button = new JButton("获取图像");
add(button,BorderLayout.NORTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
Socket s = new Socket("localhost",8888);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.print("a");
DataInputStream in = new DataInputStream(s.getInputStream());
byte[]b = new byte[1000000];
in.read(b);
ByteArrayInputStream bin = new ByteArrayInputStream(b);
BufferedImage image = ImageIO.read(bin);
ImageIO.write(image, "gif", new File("2.gif"));
s.close();
} catch (Exception e) {
}
}
});
panel = new MyPanel();
add(panel);
}
public static void main(String[] args) throws Exception {
T6Client frame = new T6Client();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
4. BufferedImage与Image之间的相互转换
<一>
BufferedImage input = ImageIO.read(file1);
Image big = input.getScaledInstance(256, 256,Image.SCALE_DEFAULT); //放缩图片
BufferedImage inputbig = new BufferedImage(256, 256,BufferedImage.TYPE_INT_BGR); inputbig.getGraphics().drawImage(input, 0, 0, 256, 256, null); //画图
<二>
public BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } image = new ImageIcon(image).getImage(); //ImageIcon与Image之间的转化: ImageIcon icon = new ImageIcon(image); image = icon.getImage(); boolean hasAlpha = false; BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { int transparency = Transparency.OPAQUE; if (hasAlpha) { transparency = Transparency.BITMASK; } GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { } if (bimage == null) { int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha) { type = BufferedImage.TYPE_INT_ARGB; } bimage = new BufferedImage(image.getWidth(null),image.getHeight(null), type); } Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }