Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > BufferedImage


Автор: Fieral 22.4.2008, 06:23
Есть изображение, нужно сделать его частично прозрачным, с плавно меняющейся прозрачностью.

Я его выливаю в 4 массива покомпонентно A R G B, а затем делаю обработку массива A (отвечающего за альфу), после чего формирую обратно изображение и вывожу на форму.

Проблема в том что те пиксели у которых альфа меньше 128 - прозрачные, а те что больше - видны полностью.
Вопрос - как сделать непрерывный переход от прозрачного к прозрачному?


Код

char A[][]
char R[][]
char G[][]
char B[][]
...
public BufferedImage GetBufferedImage() {

        int[] M = new int[this.H*this.W];
        int t=0;
        byte r,g,b,a;
        for(int i=0;i<this.H;i++){
        for(int j=0;j<this.W;j++){
                        a=(byte) (A[i][j]&0xff);
                        r=(byte) (R[i][j]&0xff);
                        g=(byte) (G[i][j]&0xff);
                        b=(byte) (B[i][j]&0xff);
                        
                        M[i*this.W+j]=(a<<24)+(r<<16)+(g<<8)+(b);
        }
        }
        return this.Image2BufferedImage(Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(this.W,this.H, M, 0, this.W)));
        
    }

Автор: LSD 22.4.2008, 12:11
Когда создаешь BufferedImage указывай тип TYPE_4BYTE_ABGR.

Автор: Fieral 22.4.2008, 12:35
Код

public BufferedImage Image2BufferedImage(Image image) {
        if (image instanceof BufferedImage) {return (BufferedImage)image;}
    
        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();
    
        // Determine if the image has transparent pixels
        boolean hasAlpha = hasAlpha(image);
    
        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha == true) {transparency = Transparency.BITMASK;}
    
            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        } 
        catch (HeadlessException e) {} //No screen
    
        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha == true) {type = BufferedImage.TYPE_4BYTE_ABGR;}
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }
    
        // Copy image to buffered image
        Graphics g = bimage.createGraphics();
    
        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();
    
        return bimage;
    }

      public boolean hasAlpha(Image image) {
             // If buffered image, the color model is readily available
             if (image instanceof BufferedImage) {return ((BufferedImage)image).getColorModel().hasAlpha();}
         
             // Use a pixel grabber to retrieve the image's color model;
             // grabbing a single pixel is usually sufficient
             PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
             try {pg.grabPixels();} catch (InterruptedException e) {}
         
             // Get the image's color model
             return pg.getColorModel().hasAlpha();
         }


а где это указывается?

Добавлено через 2 минуты и 26 секунд
Transparency.TRANSLUCENT !!!!   smile 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)