【java】BufferImage

来源:互联网 发布:淘宝怎么邮费退款 编辑:程序博客网 时间:2024/05/17 01:07
Java之BufferedImage简谈
2013年04月18日 ⁄ Java编程 ⁄ 共 6187字⁄ ⁄ 暂无评论

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;
}




源:http://ieroot.com/2013/04/18/829.html

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机进水了开不了机怎么办 苹果5s进水了怎么办修要多少钱 吃了过期3年的药怎么办 离婚后孩子的抚养费不给怎么办 小车钥匙丢了怎么办配要多少钱 一键启动的车钥匙丢了怎么办 股票退市了手里的股票怎么办 3d硬金以后要换怎么办 偏指甲红肿长在了肉里怎么办 牙齿黄怎么办教你牙齿美白小窍门 2岁孩门牙磕断了怎么办 脸上用了含激素的产品应该怎么办 身上起红疙瘩水泡很痒怎么办 怀疑老公有外遇他不承认怎么办 苹果手机锁屏密码忘了怎么办 黑色t恤洗了掉毛怎么办 老婆要和我离婚我该怎么办 发现老婆有外遇最明智的怎么办 高度近视怎么办我快一千度近 天刀耐久度为0了怎么办 新车被4s店装了怎么办 h面和w面联系线怎么办 造梦西游3到80级怎么办 加95的车加了92怎么办 95的车加92的油怎么办 宝宝六个月了奶水变少了怎么办 六个月的宝宝不爱吃奶粉怎么办 脸上挤黑头留下的小坑怎么办 小孩身上起红疙瘩很痒怎么办 肛门上长了个肉疙瘩怎么办 脚撞了一下肿了怎么办u 怀孕9个月同床了怎么办 结婚两年了都没怀孕怎么办 怀孕快40周了没有生的迹象怎么办 生完小孩掉头发很厉害怎么办 我27岁欠50多万怎么办 8岁童牙齿摔松了怎么办 我的眉毛后半边很稀少怎么办 剑三95修为满了怎么办 21三体综合症临界高风险怎么办 率土之滨没地了怎么办