Oppure

Loading
23/05/07 14:51
elettrozp
ciao a tutti,
sapete come settare questi due valori in una BufferedImage?
oppure come implementarli?

ciao
aaa
27/05/07 11:53
netarrow
In la guida completa Java 2 di shildt ho trovato questo esempio:

/*

* <applet code=ImageFilterDemo width=350 height=450>

* <param name=img value=vincent.jpg>

* <param name=filters value="Grayscale+Invert+Contrast+Blur+Sharpen">

* </applet>

*/

import java.applet.*;

import java.awt.*;
import java.awt.image.*;

import java.awt.event.*;

import java.util.*;



public class ImageFilterDemo extends Applet implements ActionListener {

Image img;

PlugInFilter pif;

Image fimg;

Image curImg;

LoadedImage lim;

Label lab;

Button reset;

public ImageFilterDemo() {}

public void init() {

setLayout(new BorderLayout());

Panel p = new Panel();

add(p, BorderLayout.SOUTH);

reset = new Button("Reset");

reset.addActionListener(this);

p.add(reset);

StringTokenizer st = new StringTokenizer(getParameter("filters"), "+");

while(st.hasMoreTokens()) {

Button b = new Button(st.nextToken());

b.addActionListener(this);

p.add(b);

}



lab = new Label("");

add(lab, BorderLayout.NORTH);



img = getImage(getDocumentBase(), getParameter("img"));

lim = new LoadedImage(img);

add(lim, BorderLayout.CENTER);



}



public void actionPerformed(ActionEvent ae) {

String a = "";



try {

a = (String)ae.getActionCommand();

if (a.equals("Reset")) {

lim.set(img);

lab.setText("Normal");

}

else {

pif = (PlugInFilter) Class.forName(a).newInstance();

fimg = pif.filter(this, img);

lim.set(fimg);

lab.setText("Filtered: " + a);

}

repaint();

} catch (ClassNotFoundException e) {

lab.setText(a + " not found");

lim.set(img);

repaint();

} catch (InstantiationException e) {

lab.setText("could't new " + a);

} catch (IllegalAccessException e) {

lab.setText("no access: " + a);

}

}

}

interface PlugInFilter {

java.awt.Image filter(java.applet.Applet a, java.awt.Image in);

}



class LoadedImage extends Canvas {

Image img;



public LoadedImage(Image i) {

set(i);

}



void set(Image i) {

MediaTracker mt = new MediaTracker(this);

mt.addImage(i, 0);

try {

mt.waitForAll();

} catch (InterruptedException e) { };

img = i;

repaint();

}



public void paint(Graphics g) {

if (img == null) {

g.drawString("no image", 10, 30);

} else {

g.drawImage(img, 0, 0, this);

}

}



public Dimension getPreferredSize() {

return new Dimension(img.getWidth(this), img.getHeight(this));

}



public Dimension getMinimumSize() {

return getPreferredSize();

}

}



class Grayscale extends RGBImageFilter implements PlugInFilter {

public Image filter(Applet a, Image in) {

return a.createImage(new FilteredImageSource(in.getSource(), this));

}



public int filterRGB(int x, int y, int rgb) {

int r = (rgb >> 16) & 0xff;

int g = (rgb >> 8) & 0xff;

int b = rgb & 0xff;

int k = (int) (.56 * g + .33 * r + .11 * b);

return (0xff000000 | k << 16 | k << 8 | k);

}

}



class Invert extends RGBImageFilter implements PlugInFilter {

public Image filter(Applet a, Image in) {

return a.createImage(new FilteredImageSource(in.getSource(), this));

}

public int filterRGB(int x, int y, int rgb) {

int r = 0xff - (rgb >> 16) & 0xff;

int g = 0xff - (rgb >> 8) & 0xff;

int b = 0xff - rgb & 0xff;

return (0xff000000 | r << 16 | g << 8 | b);

}

}




class Contrast extends RGBImageFilter implements PlugInFilter {



public Image filter(Applet a, Image in) {

return a.createImage(new FilteredImageSource(in.getSource(), this));

}



private int multclamp(int in, double factor) {

in = (int) (in * factor);

return in > 255 ? 255 : in;

}



double gain = 1.2;

private int cont(int in) {

return (in < 128) ? (int)(in/gain) : multclamp(in, gain);

}



public int filterRGB(int x, int y, int rgb) {

int r = cont((rgb >> 16) & 0xff);

int g = cont((rgb >> 8) & 0xff);

int b = cont(rgb & 0xff);

return (0xff000000 | r << 16 | g << 8 | b);

}

}



abstract class Convolver implements ImageConsumer, PlugInFilter {

int width, height;

int imgpixels[], newimgpixels[];



abstract void convolve(); // filter goes here...



public Image filter(Applet a, Image in) {

in.getSource().startProduction(this);

waitForImage();

newimgpixels = new int[width*height];



try {

convolve();

} catch (Exception e) {

System.out.println("Convolver failed: " + e);

e.printStackTrace();

}



return a.createImage(

new MemoryImageSource(width, height, newimgpixels, 0, width));

}



synchronized void waitForImage() {

try { wait(); } catch (Exception e) { };

}



public void setProperties(java.util.Hashtable dummy) { }

public void setColorModel(ColorModel dummy) { }

public void setHints(int dummy) { }



public synchronized void imageComplete(int dummy) {

notifyAll();

}



public void setDimensions(int x, int y) {

width = x;

height = y;

imgpixels = new int[x*y];

}



public void setPixels(int x1, int y1, int w, int h,

ColorModel model, byte pixels[], int off, int scansize) {

int pix, x, y, x2, y2, sx, sy;



x2 = x1+w;

y2 = y1+h;

sy = off;

for(y=y1; y<y2; y++) {

sx = sy;

for(x=x1; x<x2; x++) {

pix = model.getRGB(pixels[sx++]);

if((pix & 0xff000000) == 0)

pix = 0x00ffffff;

imgpixels[y*width+x] = pix;

}

sy += scansize;

}

}



public void setPixels(int x1, int y1, int w, int h,

ColorModel model, int pixels[], int off, int scansize) {

int pix, x, y, x2, y2, sx, sy;



x2 = x1+w;

y2 = y1+h;

sy = off;

for(y=y1; y<y2; y++) {

sx = sy;

for(x=x1; x<x2; x++) {

pix = model.getRGB(pixels[sx++]);

if((pix & 0xff000000) == 0)

pix = 0x00ffffff;

imgpixels[y*width+x] = pix;

}

sy += scansize;

}

}

}


class Blur extends Convolver {

public void convolve() {

for(int y=1; y<height-1; y++) {

for(int x=1; x<width-1; x++) {

int rs = 0;

int gs = 0;

int bs = 0;



for(int k=-1; k<=1; k++) {

for(int j=-1; j<=1; j++) {

int rgb = imgpixels[(y+k)*width+x+j];

int r = (rgb >> 16) & 0xff;

int g = (rgb >> 8) & 0xff;

int b = rgb & 0xff;

rs += r;

gs += g;

bs += b;

}

}



rs /= 9;

gs /= 9;

bs /= 9;



newimgpixels[y*width+x] = (0xff000000 |

rs << 16 | gs << 8 | bs);

}

}

}

}


class Sharpen extends Convolver {



private final int clamp(int c) {

return (c > 255 ? 255 : (c < 0 ? 0 : c));

}



public void convolve() {

int r0=0, g0=0, b0=0;

for(int y=1; y<height-1; y++) {

for(int x=1; x<width-1; x++) {

int rs = 0;

int gs = 0;

int bs = 0;



for(int k=-1; k<=1; k++) {

for(int j=-1; j<=1; j++) {

int rgb = imgpixels[(y+k)*width+x+j];

int r = (rgb >> 16) & 0xff;

int g = (rgb >> 8) & 0xff;

int b = rgb & 0xff;

if (j == 0 && k == 0) {

r0 = r;

g0 = g;

b0 = b;

} else {

rs += r;

gs += g;

bs += b;

}

}

}



rs >>= 3;

gs >>= 3;

bs >>= 3;

newimgpixels[y*width+x] = (0xff000000 |

clamp(r0+r0-rs) << 16 |

clamp(g0+g0-gs) << 8 |

clamp(b0+b0-bs));

}

}

}

}
Ultima modifica effettuata da netarrow 27/05/07 12:13
aaa
27/05/07 11:56
elettrozp
provo subito a capire il codice...
grazie netarrow:k:
aaa
27/05/07 12:18
netarrow
ho editato il sorgente in modo che sia facile da provare, ora basta che copi tutto in ImageFilterDemo.java e compili (prima era da dividere file per file).

Una volta creati i file .class crea un file .htm che contenga i tag che sono nel commento e metti vicino a file class e alla pagina html un'immagine di nome vincent.jpg (o cambi il nome anche nel sorgente html)

A questo punto avrai un applet con in alto l'immagine e sotto dei tasti tipo "contrast, blur" ecc... e vedrai le modifiche all'immagine

C'è anche l'opzione grayscale che ho visto che cercavi in un altro topic

Ultima modifica effettuata da netarrow 27/05/07 12:19
aaa