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"));
  • 相关阅读:
    OAuth2在微服务架构中的应用
    使用SpringSecurity体验OAuth2 (入门2)
    SpringSecurity的配置分析
    SpringSecurity在Springboot下使用的初步体验
    Spring框架中的org.springframework.context.annotation.Import注解类
    使用SpringSecurity体验OAUTH2之一 (入门1)
    5. SpringBoot —— Actuator简介
    无题
    C# 委托的本质
    json 字符串 反序列化
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2960847.html
Copyright © 2011-2022 走看看