• JAVA中BufferedImage的用法


    1.用到的包
    
    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类型
    
    }
    天下无难事,只要肯攀登!见多识广是多么重要!
  • 相关阅读:
    struts2的类型转换
    简单的图形验证码
    无法添加某个relationship给SAP CRM Product category的一个可能原因
    如何找到ABAP里被动态调用的update function module
    ABAP数据库表的元数据
    SAP CRM Product hierarchy,Category和Application的三个问题
    使用javap分析Java字节码的一个例子
    在SAP ABAP里使用注解@Inject模拟Java Spring
    SAP系统里的胖接口Fat interface
    如何使用ABAP代码创建SAP Product Category
  • 原文地址:https://www.cnblogs.com/Allen-Wei/p/9092064.html
Copyright © 2020-2023  润新知