JAVA中BufferedImage的用法

來源:互聯網
上載者:User

標籤:oca   output   java   level   except   todo   pix   bsp   特定   

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.如何取到BufferedImageBufferedImage 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.顯示BufferedImagepublic void paint(Graphics g){    super.paint(g);    g.drawImage(image);    //image為BufferedImage類型}

 

JAVA中BufferedImage的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.