java Graphics2D 透明背景的png图片处理后背景成了黑色
publicstaticBufferedImagerotateImg(StringsrcImagePath,intdegree,StringimageFormat)thr...
public static BufferedImage rotateImg(String srcImagePath, int degree,
String imageFormat) throws IOException {
BufferedImage image = ImageIO.read(new File(srcImagePath));
int iw = image.getWidth();// 原始图象的宽度
int ih = image.getHeight();// 原始图象的高度
int w = 0;
int h = 0;
int x = 0;
int y = 0;
degree = degree % 360;
if (degree < 0)
degree = 360 + degree;// 将角度转换到0-360度之间
double ang = Math.toRadians(degree);// 将角度转为弧度
/**
* 确定旋转后的图象的高度和宽度
*/
if (degree == 180 || degree == 0 || degree == 360) {
w = iw;
h = ih;
} else if (degree == 90 || degree == 270) {
w = ih;
h = iw;
} else {
// int d = iw + ih;
double cosVal = Math.abs(Math.cos(ang));
double sinVal = Math.abs(Math.sin(ang));
w = (int) (sinVal * ih) + (int) (cosVal * iw);
h = (int) (sinVal * iw) + (int) (cosVal * ih);
}
x = (w / 2) - (iw / 2);// 确定原点坐标
y = (h / 2) - (ih / 2);
BufferedImage rotatedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = rotatedImage.createGraphics();
rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h,
Transparency.TRANSLUCENT);
gs.dispose();
gs = rotatedImage.createGraphics();
gs.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
AffineTransform at = new AffineTransform();
at.rotate(ang, w / 2, h / 2);// 旋转图象
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at,
AffineTransformOp.TYPE_BICUBIC);
op.filter(image, rotatedImage);
image = rotatedImage;
// FileOutputStream fos = null;
// fos = new FileOutputStream("c:\\aaa.png");
// ImageIO.write(image, imageFormat, fos);
return image;
} 展开
String imageFormat) throws IOException {
BufferedImage image = ImageIO.read(new File(srcImagePath));
int iw = image.getWidth();// 原始图象的宽度
int ih = image.getHeight();// 原始图象的高度
int w = 0;
int h = 0;
int x = 0;
int y = 0;
degree = degree % 360;
if (degree < 0)
degree = 360 + degree;// 将角度转换到0-360度之间
double ang = Math.toRadians(degree);// 将角度转为弧度
/**
* 确定旋转后的图象的高度和宽度
*/
if (degree == 180 || degree == 0 || degree == 360) {
w = iw;
h = ih;
} else if (degree == 90 || degree == 270) {
w = ih;
h = iw;
} else {
// int d = iw + ih;
double cosVal = Math.abs(Math.cos(ang));
double sinVal = Math.abs(Math.sin(ang));
w = (int) (sinVal * ih) + (int) (cosVal * iw);
h = (int) (sinVal * iw) + (int) (cosVal * ih);
}
x = (w / 2) - (iw / 2);// 确定原点坐标
y = (h / 2) - (ih / 2);
BufferedImage rotatedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = rotatedImage.createGraphics();
rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h,
Transparency.TRANSLUCENT);
gs.dispose();
gs = rotatedImage.createGraphics();
gs.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
AffineTransform at = new AffineTransform();
at.rotate(ang, w / 2, h / 2);// 旋转图象
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at,
AffineTransformOp.TYPE_BICUBIC);
op.filter(image, rotatedImage);
image = rotatedImage;
// FileOutputStream fos = null;
// fos = new FileOutputStream("c:\\aaa.png");
// ImageIO.write(image, imageFormat, fos);
return image;
} 展开
1个回答
展开全部
注意参数imageFormat的设置,不同的参数图形输出处理不同,保证透明需要设置成png格式
// FileOutputStream fos = null;
// fos = new FileOutputStream("c:\\aaa.png");
// ImageIO.write(image, imageFormat, fos);
imageFormt = “png”
// FileOutputStream fos = null;
// fos = new FileOutputStream("c:\\aaa.png");
// ImageIO.write(image, imageFormat, fos);
imageFormt = “png”
追问
没错我设置成了png,不行的
追答
测试没有发现问题。
解开最后被注释掉的3行。
调用:rotateImg("f:\\aaa_old.png", 90, "png"); 正确生成了透明效果的图片
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询