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"));
  • 相关阅读:
    基于MySQL提供的Yum repository安装MySQL5.6
    CentOS中无法使用setup命令 -bash:setup: command not found
    jdk8新特性-亮瞎眼的lambda表达式
    Git branch 分支与合并分支
    (转)Hashtable与ConcurrentHashMap区别
    java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition的解决方案
    一些面试问题以及一些解法
    ipython的使用
    复习点算法知识,水仙花数加冒泡排序,以及一道算法题
    一些部署django用到的linux命令
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2960847.html
Copyright © 2011-2022 走看看