Gui*_*meA 79 java transparency alpha image-processing
我需要在Java中合并两个图像(BufferedImage).如果没有透明度,这不会是一个问题.基本图像已经具有一定的透明度.我希望保持原样并对其应用"掩码",即第二张图像.第二个图像没有不透明像素,实际上它几乎完全透明,只是有一些不太透明的像素来提供某种"光效",就像反射一样.重要细节:我不想在屏幕上执行此操作,使用图形,我需要获得带有合并的BufferedImage.
谁能帮我?谢谢!
细节:合并两个保持透明度的图像.这就是我需要做的.
注意:Java中的Set BufferedImage alpha掩码不能满足我的要求,因为它不能很好地处理具有透明度的两个图像 - 它会修改第一个图像透明度.
Pet*_*ser 173
只需创建一个具有透明度的新BufferedImage,然后在其上绘制另外两个图像(具有完整或半透明).这是它的样子:
示例代码(图像称为'image.png'和'overlay.png'):
File path = ... // base path of the images
// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
Run Code Online (Sandbox Code Playgroud)
垂直合并任何类型的文件。
void mergeFiles(List<String> files, String fileName) {
int heightTotal = 0;
int maxWidth = 100;
List<BufferedImage> images = new ArrayList<>();
try {
for (String file : files) {
BufferedImage image = ImageIO.read(new File(file));
images.add(image);
}
for (BufferedImage bufferedImage : images) {
heightTotal += bufferedImage.getHeight();
if (bufferedImage.getWidth() > maxWidth) {
maxWidth = bufferedImage.getWidth();
}
}
int heightCurr = 0;
BufferedImage concatImage = new BufferedImage(maxWidth, heightTotal, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = concatImage.createGraphics();
for (BufferedImage bufferedImage : images) {
g2d.drawImage(bufferedImage, 0, heightCurr, null);
heightCurr += bufferedImage.getHeight();
}
File compressedImageFile = new File(fileName);
OutputStream outputStream = new FileOutputStream(compressedImageFile);
float imageQuality = 0.7f;
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("jpeg");
if (!imageWriters.hasNext())
throw new IllegalStateException("Writers Not Found!!");
ImageWriter imageWriter = imageWriters.next();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
//Set the compress quality metrics
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(imageQuality);
//Created image
imageWriter.write(null, new IIOImage(concatImage, null, null), imageWriteParam);
// close all streams
outputStream.close();
imageOutputStream.close();
imageWriter.dispose();
log.info(" Files Merged");
} catch (IOException e) {
log.error("Error while merging files :::"+e);
throw new RuntimeException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
84971 次 |
最近记录: |