我想带来图像透明度的特殊部分。我怎么能在java中这样做?我尝试了一些代码:
public BufferedImage[] splitBackgroundImages() throws IOException {
int count = 0;
bimages = new BufferedImage[chuncks];
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
// Initialize the image array with image chunks
bimages[count] = image.getSubimage(x * this.chunckwidth, y
* this.chunckheigth, this.chunckwidth,
this.chunckheigth);
Graphics2D gr = bimages[count].createGraphics();
gr.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1));
//gr.clearRect(5, 5, chunckwidth-10, chunckheigth -10);
Color transparent = new Color(0,0,0,0);
gr.setPaint(transparent);
gr.setBackground(transparent);
gr.fillRect(5, 5, chunckwidth-10, chunckheigth-10);
/*bimages[count] = gr.getDeviceConfiguration().createCompatibleImage(
chunckwidth, chunckheigth, Transparency.TRANSLUCENT);
*/
gr.fillRect(5, 5, chunckwidth-10, chunckheigth-10);
gr.drawImage(bimages[count++], 0, 0, chunckwidth, chunckheigth,
chunckwidth * y, chunckheigth * x, chunckwidth * y
+ chunckwidth, chunckheigth * x + chunckwidth,
null);
gr.dispose();
}
答案 0 :(得分:2)
好的,我放弃,或者更加无聊。
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class HoleRing {
HoleRing(BufferedImage image) {
// presumes the images are identical in size BNI
int w = image.getWidth();
int h = image.getHeight();
Ellipse2D.Double ellipse1 = new Ellipse2D.Double(
w/16,h/16,7*w/8,7*h/8);
Ellipse2D.Double ellipse2 = new Ellipse2D.Double(
w/4,h/4,w/2,h/2);
Area circle = new Area(ellipse1);
circle.subtract(new Area(ellipse2));
BufferedImage result = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setClip(circle);
g.drawImage(image, 0, 0, null);
g.setClip(null);
Stroke s = new BasicStroke(2);
g.setStroke(s);
g.setColor(Color.BLACK);
g.draw(circle);
g.dispose();
JLabel l = new JLabel(new ImageIcon(result));
JOptionPane.showMessageDialog(null, l);
}
public static void main(String[] args) throws Exception {
URL url = new URL("http://pscode.org/media/stromlo1.jpg");
final BufferedImage bi = ImageIO.read(url);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new HoleRing(bi);
}
});
}
}
答案 1 :(得分:1)
不确定我理解这个问题。但是,仅绘制BufferedImage
的一部分的简单方法是使用以下代码:
BufferedImage bi = [ ... ];
Graphics2D g = bi.createGraphics();
g.setClip(x, y, width, height);
否则,如果您想删除图片的一部分,可以使用g.clip(shape)
或g.clipRect(x, y, width, height)
,这将删除一个矩形。