zoukankan      html  css  js  c++  java
  • BufferedImage学习记录一

    一、BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。

    二、java创建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);
        }
    }

    三、获取图片:

    BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));

    如:

    BufferedImage image = ImageIO.read(new File("1.gif"));
  • 相关阅读:
    python爬虫专栏学习
    Scrapy学习笔记
    《The Python Standard Library》——http模块阅读笔记2
    《The Python Standard Library》——http模块阅读笔记1
    《The Python Tutorial》——Errors and Exceptions 阅读笔记
    web服务器架构演化及所其技术知识体系(分布式的由来)
    django学习笔记——搭建博客网站
    《计算机系统要素》学习笔记
    python查看模块版本及所在文件夹
    网络编程---笔记2
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2960847.html
Copyright © 2011-2022 走看看