JAVA中BufferedImage的用法

转载:(3条消息) JAVA中BufferedImage的用法_梦醒何处_的博客-CSDN博客_bufferedimage

BufferedImage是一个内存对象,当通过ImageIO.read()方法读取一个图像文件时,读取到的关于图像文件的所有信息都会被存储在该API返回的BufferedImage内存对象中

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

  }

2.如何取到BufferedImage

BufferedImage image = ImageIO.read(new File("1.gif"));

3. BufferedImage  ---->byte[]

ImageIO.write(BufferedImage image,String format,OutputStream out);方法可以很好的解决问题;

参数image表示获得的BufferedImage;

参数format表示图片的格式,比如“gif”等;

参数out表示输出流,如果要转成Byte数组,则输出流为ByteArrayOutputStream即可;

执行完后,只需要toByteArray()就能得到byte[];

4.显示BufferedImage

public void paint(Graphics g){

    super.paint(g);

    g.drawImage(image);    //image为BufferedImage类型

}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将多张图片合成一张,可以使用JavaBufferedImage类。以下是实现该功能的基本步骤: 1. 创建一个新的BufferedImage对象,指定它的宽度和高度。 2. 获取Graphics2D对象,并使用它来将多张图片绘制到新的BufferedImage上。可以使用Graphics2D的drawImage方法来绘制图片。 3. 将合成的BufferedImage保存到磁盘或将其显示在界面上。 以下是一个简单的Java代码示例,演示如何将两张图片合成一张: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class MergeImages { public static void main(String[] args) throws Exception { BufferedImage img1 = ImageIO.read(new File("image1.jpg")); BufferedImage img2 = ImageIO.read(new File("image2.jpg")); int width = img1.getWidth() + img2.getWidth(); int height = Math.max(img1.getHeight(), img2.getHeight()); BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = combined.createGraphics(); g.drawImage(img1, 0, 0, null); g.drawImage(img2, img1.getWidth(), 0, null); g.dispose(); ImageIO.write(combined, "PNG", new File("merged.png")); } } ``` 在这个例子,我们使用了ImageIO类来读取两张图片,然后计算出新的合成图片的宽度和高度。接下来,我们创建了一个新的BufferedImage对象,指定它的宽度和高度。然后,我们获取Graphics2D对象,并使用它来将两张图片绘制到新的BufferedImage上。最后,我们将合成的BufferedImage保存到磁盘上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值