java 创设PNG格式的透明背景的图片(转载)

java 创建PNG格式的透明背景的图片(转载)
1. 生成png图片

int width = 400;

int height = 300;

// 创建BufferedImage对象

BufferedImage image = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);

// 获取Graphics2D

Graphics2D g2d = image.createGraphics();

// 画图

g2d.setColor(new Color(255,0,0));

g2d.setStroke(new BasicStroke(1));

g2d.draw

//释放对象

g2d.dispose();

// 保存文件   

ImageIO.write(image, "png", new File("c:/test.png"));

int width = 400;

int height = 300;

// 创建BufferedImage对象

BufferedImage image = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);

// 获取Graphics2D

Graphics2D g2d = image.createGraphics();

// 画图

g2d.setColor(new Color(255,0,0));

g2d.setStroke(new BasicStroke(1));

g2d.draw

//释放对象

g2d.dispose();

// 保存文件   

ImageIO.write(image, "png", new File("c:/test.png"));

这只是绘制图形的代码,其背景是黑色的,如何才能背景透明呢?继续搜索,没有得到结果,不过搜出以下代码,它只是把自己绘制的图形设置为透明或半透明,背景并不透明,如下:

2. 绘制半透明图形

int width = 400;

int height = 300;

// 创建BufferedImage对象

BufferedImage image = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);

// 获取Graphics2D

Graphics2D g2d = image.createGraphics();

// 设置透明度

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f)); // 1.0f为透明度 ,值从0-1.0,依次变得不透明

// 画图

g2d.setColor(new Color(255,0,0));

g2d.setStroke(new BasicStroke(1));

g2d.draw

//释放对象

//透明度设置 结束

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

g2d.dispose();

// 保存文件   

ImageIO.write(image, "png", new File("c:/test.png"));

这样绘制的图形应该说是前景透明的,背景依然是黑色,:(

查看了BufferedImage 和Graphics2D 所有方法和属性,终于找到了解决方案,只不过是增加两行代码而已,如下:

int width = 400;

int height = 300;

// 创建BufferedImage对象

BufferedImage image = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);

// 获取Graphics2D

Graphics2D g2d = image.createGraphics();

// ---------- 增加下面的代码使得背景透明 -----------------

image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);

g2d.dispose();

g2d = image.createGraphics();

// ---------- 背景透明代码结束 -----------------

// 画图

g2d.setColor(new Color(255,0,0));

g2d.setStroke(new BasicStroke(1));

g2d.draw

//释放对象

g2d.dispose();

// 保存文件   

ImageIO.write(image, "png", new File("c:/test.png"));

================================================

白背景变透明

package com.goldgrid.socket.client;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

public class Alpha {

    protected static Logger logger = Logger.getLogger(ClientSocket.class);

public byte[] transferAlpha(Image image) {

   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

   try {
    ImageIcon imageIcon = new ImageIcon(image);
    BufferedImage bufferedImage = new BufferedImage(imageIcon
      .getIconWidth(), imageIcon.getIconHeight(),
      BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
    g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
      .getImageObserver());
    int alpha = 0;
    for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage
      .getHeight(); j1++) {
     for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage
       .getWidth(); j2++) {
      int rgb = bufferedImage.getRGB(j2, j1);
    
      int R =(rgb & 0xff0000 ) >> 16 ;
      int G= (rgb & 0xff00 ) >> 8 ;
      int B= (rgb & 0xff );
      if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){
       rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
      }

      bufferedImage.setRGB(j2, j1, rgb);

     }
    }

    g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
    //ImageIO.write(bufferedImage, "png", new File("d:/test.png.png"));

    ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
   } catch (Exception e) {
    logger.error(e.toString());
   }finally{
   
   }

   return byteArrayOutputStream.toByteArray();

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值