Java给透明图片加背景色的问题

yu_xian 2009-05-22 09:20:37

我想实现一个给透明图片添加背景色的功能,可是添加的颜色总是和设置的不一样,而且有时候图片不透明的部分颜色还变了一点,大家看看是什么问题啊,下面是我的这个方法:

public static int add(String image, Color backGroundColor, String resultImage,int x,int y) {
BufferedImage myImage = null;
try {
myImage = ImageIO.read(new File(image));
} catch (IOException e) {
e.printStackTrace();
return -1;
}
Graphics2D g = myImage.createGraphics();
g.drawImage(myImage, x, y,backGroundColor, null);

FileOutputStream out = null;

try {
out = new FileOutputStream(resultImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (IOException e) {
e.printStackTrace();
return -1;
}

return 1;
}
...全文
1352 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
talentluo 2009-06-05
  • 打赏
  • 举报
回复
g.drawImage(myImage, x, y,backGroundColor, null); 

这代码画出的图片始终是黑色的。即使使用了setColor指定了别的颜色,图片背景还是黑色,但graphics的getColor得到的已经是setColor()指定的颜色了。
需要变换下,使用fillRect来填充。只要调用了下面这句,图片的背景色就会变为任何setColor指定的颜色。
graphics.fillRect(0, 0, width, height);
yu_xian 2009-05-25
  • 打赏
  • 举报
回复
自己顶,有没有高手知道啊?????
zhangkang0808 2009-05-23
  • 打赏
  • 举报
回复
学习!
w40338544 2009-05-22
  • 打赏
  • 举报
回复
学习下,帮你顶顶
yu_xian 2009-05-22
  • 打赏
  • 举报
回复
透明图片和文字是不一样的,
g.setColor(markContentColor);
g.setBackground(Color.white);
这两句用透明图片覆盖了就没有任何效果了。

我用过透明图片覆盖到另一张图片上就没事,
现在就是背景色不一致,设置蓝色的背景就成青色了,红色的就变成紫色了,不知道为什么会变成这样。
javaboy2006 2009-05-22
  • 打赏
  • 举报
回复
lz试试这段:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
import java.text.AttributedString;
import java.awt.font.TextAttribute;
import java.text.AttributedCharacterIterator;

public class WaterMark {
/**
* 给图片添加水印文字
*
* @param filePath
* 需要添加水印的图片的路径
* @param markContent
* 水印的文字
* @param markContentColor
* 水印文字的颜色
* @param qualNum
* 图片质量
* @param fontType
* 字体
* @param fontsize
* 字体大小
* @return
*/
public BufferedImage createMark(String filePath, String markContent,
Color markContentColor, float qualNum, String fontType,
int fontSize, boolean isAppendWatermark) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);

// 为图像添加水印文字
if (isAppendWatermark == true) {
AttributedString ats = new AttributedString(markContent);
Font f = new Font(fontType, Font.BOLD, fontSize);

ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();

g.drawString(iter, width / 5, height / 5); // 添加水印的文字和设置水印文字出现的内容
}

g.dispose();

try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return bimage;
}
}
yu_xian 2009-05-22
  • 打赏
  • 举报
回复
谢谢楼上的,我已经做好了一个在图片上覆盖透明图片的,可是就是想根据传的颜色来生成新的,如果实在没办法解决就只能做一个模版图片了,不过为什么会有这种现象,不搞清楚心里也不舒服。
dracularking 2009-05-22
  • 打赏
  • 举报
回复
试试反过来在背景色图片上加透明图

/**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetImg --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
* @param alpha
* --透明度
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y, float alpha) {
try {
// 目标文件
float Alpha = alpha;
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
Alpha));
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧