使用 AffineTransform 剪切 BufferedImage 时出现问题
Problems shearing BufferedImage with AffineTransform
我想在 Java 中使用 AffineTransform 剪切我的图像。如果我这样做,我总是会得到黑色边界。
for (File input : inputImages) {
if (!input.getName().contains(".DS_Store")) {
BufferedImage buffer = ImageIO.read(input);
for (int i = 0; i <= 2; i++) {
AffineTransform tx = new AffineTransform();
tx.translate(buffer.getHeight() / 2, buffer.getWidth() / 2);
tx.shear(0.3, 0);
tx.translate(-buffer.getWidth() / 2, -buffer.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage = new BufferedImage(buffer.getHeight(), buffer.getWidth(), buffer.getType());
op.filter(buffer, newImage);
File output = new File("output/" + FilenameUtils.getBaseName(input.getName()) + i + ".png");
ImageIO.write(newImage, "png", output);
}
}
}
是否有办法避免这些黑色边界并获得白色或透明背景?
使用BufferedImage.TYPE_INT_ARGB
作为图像类型创建透明图像
If I use TYPE_ARGB my output image is completely transparent
适用于更多
BufferedImage buffer = ImageIO.read(...);
AffineTransform tx = new AffineTransform();
tx.translate(buffer.getHeight() / 2, buffer.getWidth() / 2);
tx.shear(0.3, 0);
tx.translate(-buffer.getWidth() / 2, -buffer.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage = new BufferedImage(buffer.getHeight(), buffer.getWidth(), BufferedImage.TYPE_INT_ARGB);
op.filter(buffer, newImage);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(newImage)));
如果您愿意,可以使用默认颜色填充 newImage
我想在 Java 中使用 AffineTransform 剪切我的图像。如果我这样做,我总是会得到黑色边界。
for (File input : inputImages) {
if (!input.getName().contains(".DS_Store")) {
BufferedImage buffer = ImageIO.read(input);
for (int i = 0; i <= 2; i++) {
AffineTransform tx = new AffineTransform();
tx.translate(buffer.getHeight() / 2, buffer.getWidth() / 2);
tx.shear(0.3, 0);
tx.translate(-buffer.getWidth() / 2, -buffer.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage = new BufferedImage(buffer.getHeight(), buffer.getWidth(), buffer.getType());
op.filter(buffer, newImage);
File output = new File("output/" + FilenameUtils.getBaseName(input.getName()) + i + ".png");
ImageIO.write(newImage, "png", output);
}
}
}
是否有办法避免这些黑色边界并获得白色或透明背景?
使用BufferedImage.TYPE_INT_ARGB
作为图像类型创建透明图像
If I use TYPE_ARGB my output image is completely transparent
适用于更多
BufferedImage buffer = ImageIO.read(...);
AffineTransform tx = new AffineTransform();
tx.translate(buffer.getHeight() / 2, buffer.getWidth() / 2);
tx.shear(0.3, 0);
tx.translate(-buffer.getWidth() / 2, -buffer.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage = new BufferedImage(buffer.getHeight(), buffer.getWidth(), BufferedImage.TYPE_INT_ARGB);
op.filter(buffer, newImage);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(newImage)));
如果您愿意,可以使用默认颜色填充 newImage