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"));
  • 相关阅读:
    Linux零碎知识
    Xshell连接不上Linux
    Python中获取当前时间 获取当前时间前几天的代码
    重新设置Linux文件共享密码..
    【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)
    应该怎么理解 app = Flask(__name__)
    刚才在windows下发现拖拽不了文件了
    Hadoop点滴-何时使用hadoop fs、hadoop dfs与hdfs dfs命令
    Hadoop点滴-Hadoop分布式文件系统
    Hadoop点滴-初识MapReduce(2)
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2960847.html
Copyright © 2011-2022 走看看