BufferedImage aus einem Array erzeugen

teppi

Erfahrenes Mitglied
Nabend,

ich habe es nun schon eine Weile selbst versucht, aber ich komme einfach nicht drauf .. Ich habe ein Graustufenbild als BufferedImage. Aus diesem Bild ziehe ich mit folgendem Code:

Code:
Raster r = src.getRaster();
		
        int width = r.getWidth(), height = r.getHeight();
		
        int size = width*height;
		
        int pixels[] = r.getPixels(0, 0, width, height, (int[])null);
		
        int length = pixels.length;
		
        // Negativwerte berechnen
        for (int i=0; i<length; i++) {
            
			int p = pixels[i];
   
			pixels[i] = 255 - p;
				
      }

.. ein Array heraus, welches die Farbwerte des gesamten Bildes als int werte abspeichert und daraus die Negativwerte berechnet. Nun habe ich aber das Problem, das ich nicht weiß, wie man daraus wieder ein BufferedImage macht ..

Kann mir vielleicht wer helfen ? Es muss auf jeden Fall ein BufferedImage sein, weil ich die Schnittstelle BufferedImageOp laut Aufgabenstellung implementieren muss .. :)

Vielen Dank,
Stefan
 
Nun gut habs jetzt doch alleine hinbekommen ... siehe Code .. :)

Code:
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
		
	Raster r = src.getRaster();
		
        int width = r.getWidth(), height = r.getHeight();
		
        int size = width*height;
		
        int pixels[] = r.getPixels(0, 0, width, height, (int[])null);
		
        int length = pixels.length;
		
	byte pixelsAsByte[] = new byte[pixels.length];
		
        // Statistische Parameter berechnen
        for (int i=0; i<length; i++) {
            
			int p = pixels[i];
   
			pixels[i] = 255 - p;
			
			pixelsAsByte[i] = (byte) pixels[i];
				
        }
		
	   BufferedImage fBufferedImage =
		       new BufferedImage (width, height, BufferedImage.TYPE_BYTE_GRAY);
		
	   WritableRaster wr = fBufferedImage.getRaster();
	   
	   wr.setDataElements(0,0, width, height, pixelsAsByte);
		
	   return fBufferedImage;

}
 
Hallo!

Schau dir doch mal MemoryImageSource an... damit kannst du die Pixelwerte direkt über ein Array Manipulieren ohne immer ein BufferedImage erzeugen zu müssen.

Hier mal ein Beispiel was exemplarisch verdeutlicht wie ich mir das vorstelle:
Code:
/**
 * 
 */
package de.tutorials;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;

import javax.swing.JFrame;

/**
 * @author Administrator
 * 
 */
public class BufferedImageExample extends JFrame {

    private final int WIDTH = 160;

    private final int HEIGHT = 120;

    private BufferStrategy strategy;

    private MemoryImageSource mis;

    private Image img;

    private int pix[] = new int[WIDTH * HEIGHT];

    private Thread runner = new Thread() {
        public void run() {

            init();
            int idx, dx, dy = dx = idx = 0;

            int red;
            int blue;

            while (true) {

                Graphics g = strategy.getDrawGraphics();

                idx = 0;
                dy += 2;
                dx += 2;

                for (int y = 0; y < HEIGHT; y++) {

                    for (int x = 0; x < WIDTH; x++) {

                        red = (int) (180 + 180
                                * Math.sin((x + dx)
                                        / (37 + 15 * Math.sin((y + dy) / 74)))
                                * Math.sin((y + dy)
                                        / (31 + 11 * Math.sin((x + dx) / 37))));
                        blue = (int) (180 + 180
                                * Math.cos((x + dx)
                                        / (37 + 15 * Math.cos((y + dy) / 74)))
                                * Math.cos((y + dy)
                                        / (31 + 11 * Math.cos((x + dx) / 37))));

                        pix[idx++] = (16 << 24) | (red << 16) | blue;

                    }
                }

                mis.newPixels(0, 0, WIDTH, HEIGHT);

                g.drawImage(img, 0, 0, null);
                g.dispose();

                strategy.show();

                try {
                    sleep(10L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        private void init() {
            int idx = 0;
            for (int y = 0; y < HEIGHT; y++) {
                int red = (y * 255) / (HEIGHT - 1);
                for (int x = 0; x < WIDTH; x++) {
                    int blue = (x * 255) / (WIDTH - 1);
                    pix[idx++] = (255 << 24) | (red << 16) | blue;
                }
            }
            mis = new MemoryImageSource(WIDTH, HEIGHT, pix, 0, WIDTH);
            mis.setAnimated(true);
            img = createImage(mis);
        }
    };

    public BufferedImageExample() {
        super("BufferedImageExample");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setVisible(true);
        createBufferStrategy(2);
        strategy = getBufferStrategy();
        runner.start();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new BufferedImageExample();
    }

}

Ich gebe zu den Code kann man nicht gerade als "optimal" Bezeichnen aber der Effekt ist ganz nett :)

--Mit ner LookUp Table geht's ein wenig flotter ;-)

Gruß Tom
 
Zurück