-2

我想带一个图像透明度的特殊部分。我怎么能在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();
            }
4

2 回答 2

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);
            }
        });
    }
}
于 2012-08-27T12:30:12.187 回答
1

Not sure I understand the question. However, an easy way to only draw part of a BufferedImage is to use the following code:

BufferedImage bi = [ ... ];
Graphics2D g = bi.createGraphics();
g.setClip(x, y, width, height);

Or else, if you want to remove a part of an image, you can use either g.clip(shape), or g.clipRect(x, y, width, height), which will remove a Rectangle.

于 2012-08-27T07:23:25.010 回答