Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Brigthness e contrast
Forum - Java - Brigthness e contrast

Avatar
elettrozp (Normal User)
Newbie


Messaggi: 9
Iscritto: 13/05/2007

Segnala al moderatore
Postato alle 16:51
Mercoledė, 23/05/2007
ciao a tutti,
sapete come settare questi due valori in una BufferedImage?
oppure come implementarli?

ciao

PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 13:53
Domenica, 27/05/2007
In la guida completa Java 2 di shildt ho trovato questo esempio:

Codice sorgente - presumibilmente Java

  1. /*
  2.  
  3. * <applet code=ImageFilterDemo width=350 height=450>
  4.  
  5. * <param name=img value=vincent.jpg>
  6.  
  7. * <param name=filters value="Grayscale+Invert+Contrast+Blur+Sharpen">
  8.  
  9. * </applet>
  10.  
  11. */
  12.  
  13. import java.applet.*;
  14.  
  15. import java.awt.*;
  16. import java.awt.image.*;
  17.  
  18. import java.awt.event.*;
  19.  
  20. import java.util.*;
  21.  
  22.  
  23.  
  24. public class ImageFilterDemo extends Applet implements ActionListener {
  25.  
  26. Image img;
  27.  
  28. PlugInFilter pif;
  29.  
  30. Image fimg;
  31.  
  32. Image curImg;
  33.  
  34. LoadedImage lim;
  35.  
  36. Label lab;
  37.  
  38. Button reset;
  39.  
  40. public ImageFilterDemo() {}
  41.  
  42. public void init() {
  43.  
  44. setLayout(new BorderLayout());
  45.  
  46. Panel p = new Panel();
  47.  
  48. add(p, BorderLayout.SOUTH);
  49.  
  50. reset = new Button("Reset");
  51.  
  52. reset.addActionListener(this);
  53.  
  54. p.add(reset);
  55.  
  56. StringTokenizer st = new StringTokenizer(getParameter("filters"), "+");
  57.  
  58. while(st.hasMoreTokens()) {
  59.  
  60. Button b = new Button(st.nextToken());
  61.  
  62. b.addActionListener(this);
  63.  
  64. p.add(b);
  65.  
  66. }
  67.  
  68.  
  69.  
  70. lab = new Label("");
  71.  
  72. add(lab, BorderLayout.NORTH);
  73.  
  74.  
  75.  
  76. img = getImage(getDocumentBase(), getParameter("img"));
  77.  
  78. lim = new LoadedImage(img);
  79.  
  80. add(lim, BorderLayout.CENTER);
  81.  
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88. public void actionPerformed(ActionEvent ae) {
  89.  
  90. String a = "";
  91.  
  92.  
  93.  
  94. try {
  95.  
  96. a = (String)ae.getActionCommand();
  97.  
  98. if (a.equals("Reset")) {
  99.  
  100. lim.set(img);
  101.  
  102. lab.setText("Normal");
  103.  
  104. }
  105.  
  106. else {
  107.  
  108. pif = (PlugInFilter) Class.forName(a).newInstance();
  109.  
  110. fimg = pif.filter(this, img);
  111.  
  112. lim.set(fimg);
  113.  
  114. lab.setText("Filtered: " + a);
  115.  
  116. }
  117.  
  118. repaint();
  119.  
  120.  
  121. lab.setText(a + " not found");
  122.  
  123. lim.set(img);
  124.  
  125. repaint();
  126.  
  127.  
  128. lab.setText("could't new " + a);
  129.  
  130.  
  131. lab.setText("no access: " + a);
  132.  
  133. }
  134.  
  135. }
  136.  
  137. }
  138.  
  139. interface PlugInFilter {
  140.  
  141. java.awt.Image filter(java.applet.Applet a, java.awt.Image in);
  142.  
  143. }
  144.  
  145.  
  146.  
  147. class LoadedImage extends Canvas {
  148.  
  149. Image img;
  150.  
  151.  
  152.  
  153. public LoadedImage(Image i) {
  154.  
  155. set(i);
  156.  
  157. }
  158.  
  159.  
  160.  
  161. void set(Image i) {
  162.  
  163. MediaTracker mt = new MediaTracker(this);
  164.  
  165. mt.addImage(i, 0);
  166.  
  167. try {
  168.  
  169. mt.waitForAll();
  170.  
  171. } catch (InterruptedException e) { };
  172.  
  173. img = i;
  174.  
  175. repaint();
  176.  
  177. }
  178.  
  179.  
  180.  
  181. public void paint(Graphics g) {
  182.  
  183. if (img == null) {
  184.  
  185. g.drawString("no image", 10, 30);
  186.  
  187. } else {
  188.  
  189. g.drawImage(img, 0, 0, this);
  190.  
  191. }
  192.  
  193. }
  194.  
  195.  
  196.  
  197. public Dimension getPreferredSize() {
  198.  
  199. return new Dimension(img.getWidth(this), img.getHeight(this));
  200.  
  201. }
  202.  
  203.  
  204.  
  205. public Dimension getMinimumSize() {
  206.  
  207. return getPreferredSize();
  208.  
  209. }
  210.  
  211. }
  212.  
  213.  
  214.  
  215. class Grayscale extends RGBImageFilter implements PlugInFilter {
  216.  
  217. public Image filter(Applet a, Image in) {
  218.  
  219. return a.createImage(new FilteredImageSource(in.getSource(), this));
  220.  
  221. }
  222.  
  223.  
  224.  
  225. public int filterRGB(int x, int y, int rgb) {
  226.  
  227. int r = (rgb >> 16) & 0xff;
  228.  
  229. int g = (rgb >> 8) & 0xff;
  230.  
  231. int b = rgb & 0xff;
  232.  
  233. int k = (int) (.56 * g + .33 * r + .11 * b);
  234.  
  235. return (0xff000000 | k << 16 | k << 8 | k);
  236.  
  237. }
  238.  
  239. }
  240.  
  241.  
  242.  
  243. class Invert extends RGBImageFilter implements PlugInFilter {
  244.  
  245. public Image filter(Applet a, Image in) {
  246.  
  247. return a.createImage(new FilteredImageSource(in.getSource(), this));
  248.  
  249. }
  250.  
  251. public int filterRGB(int x, int y, int rgb) {
  252.  
  253. int r = 0xff - (rgb >> 16) & 0xff;
  254.  
  255. int g = 0xff - (rgb >> 8) & 0xff;
  256.  
  257. int b = 0xff - rgb & 0xff;
  258.  
  259. return (0xff000000 | r << 16 | g << 8 | b);
  260.  
  261. }
  262.  
  263. }
  264.  
  265.  
  266.  
  267.  
  268. class Contrast extends RGBImageFilter implements PlugInFilter {
  269.  
  270.  
  271.  
  272. public Image filter(Applet a, Image in) {
  273.  
  274. return a.createImage(new FilteredImageSource(in.getSource(), this));
  275.  
  276. }
  277.  
  278.  
  279.  
  280. private int multclamp(int in, double factor) {
  281.  
  282. in = (int) (in * factor);
  283.  
  284. return in > 255 ? 255 : in;
  285.  
  286. }
  287.  
  288.  
  289.  
  290. double gain = 1.2;
  291.  
  292. private int cont(int in) {
  293.  
  294. return (in < 128) ? (int)(in/gain) : multclamp(in, gain);
  295.  
  296. }
  297.  
  298.  
  299.  
  300. public int filterRGB(int x, int y, int rgb) {
  301.  
  302. int r = cont((rgb >> 16) & 0xff);
  303.  
  304. int g = cont((rgb >> 8) & 0xff);
  305.  
  306. int b = cont(rgb & 0xff);
  307.  
  308. return (0xff000000 | r << 16 | g << 8 | b);
  309.  
  310. }
  311.  
  312. }
  313.  
  314.  
  315.  
  316. abstract class Convolver implements ImageConsumer, PlugInFilter {
  317.  
  318. int width, height;
  319.  
  320. int imgpixels[], newimgpixels[];
  321.  
  322.  
  323.  
  324. abstract void convolve(); // filter goes here...
  325.  
  326.  
  327.  
  328. public Image filter(Applet a, Image in) {
  329.  
  330. in.getSource().startProduction(this);
  331.  
  332. waitForImage();
  333.  
  334. newimgpixels = new int[width*height];
  335.  
  336.  
  337.  
  338. try {
  339.  
  340. convolve();
  341.  
  342. } catch (Exception e) {
  343.  
  344. System.out.println("Convolver failed: " + e);
  345.  
  346. e.printStackTrace();
  347.  
  348. }
  349.  
  350.  
  351.  
  352. return a.createImage(
  353.  
  354. new MemoryImageSource(width, height, newimgpixels, 0, width));
  355.  
  356. }
  357.  
  358.  
  359.  
  360. synchronized void waitForImage() {
  361.  
  362. try { wait(); } catch (Exception e) { };
  363.  
  364. }
  365.  
  366.  
  367.  
  368. public void setProperties(java.util.Hashtable dummy) { }
  369.  
  370. public void setColorModel(ColorModel dummy) { }
  371.  
  372. public void setHints(int dummy) { }
  373.  
  374.  
  375.  
  376. public synchronized void imageComplete(int dummy) {
  377.  
  378. notifyAll();
  379.  
  380. }
  381.  
  382.  
  383.  
  384. public void setDimensions(int x, int y) {
  385.  
  386. width = x;
  387.  
  388. height = y;
  389.  
  390. imgpixels = new int[x*y];
  391.  
  392. }
  393.  
  394.  
  395.  
  396. public void setPixels(int x1, int y1, int w, int h,
  397.  
  398. ColorModel model, byte pixels[], int off, int scansize) {
  399.  
  400. int pix, x, y, x2, y2, sx, sy;
  401.  
  402.  
  403.  
  404. x2 = x1+w;
  405.  
  406. y2 = y1+h;
  407.  
  408. sy = off;
  409.  
  410. for(y=y1; y<y2; y++) {
  411.  
  412. sx = sy;
  413.  
  414. for(x=x1; x<x2; x++) {
  415.  
  416. pix = model.getRGB(pixels[sx++]);
  417.  
  418. if((pix & 0xff000000) == 0)
  419.  
  420. pix = 0x00ffffff;
  421.  
  422. imgpixels[y*width+x] = pix;
  423.  
  424. }
  425.  
  426. sy += scansize;
  427.  
  428. }
  429.  
  430. }
  431.  
  432.  
  433.  
  434. public void setPixels(int x1, int y1, int w, int h,
  435.  
  436. ColorModel model, int pixels[], int off, int scansize) {
  437.  
  438. int pix, x, y, x2, y2, sx, sy;
  439.  
  440.  
  441.  
  442. x2 = x1+w;
  443.  
  444. y2 = y1+h;
  445.  
  446. sy = off;
  447.  
  448. for(y=y1; y<y2; y++) {
  449.  
  450. sx = sy;
  451.  
  452. for(x=x1; x<x2; x++) {
  453.  
  454. pix = model.getRGB(pixels[sx++]);
  455.  
  456. if((pix & 0xff000000) == 0)
  457.  
  458. pix = 0x00ffffff;
  459.  
  460. imgpixels[y*width+x] = pix;
  461.  
  462. }
  463.  
  464. sy += scansize;
  465.  
  466. }
  467.  
  468. }
  469.  
  470. }
  471.  
  472.  
  473. class Blur extends Convolver {
  474.  
  475. public void convolve() {
  476.  
  477. for(int y=1; y<height-1; y++) {
  478.  
  479. for(int x=1; x<width-1; x++) {
  480.  
  481. int rs = 0;
  482.  
  483. int gs = 0;
  484.  
  485. int bs = 0;
  486.  
  487.  
  488.  
  489. for(int k=-1; k<=1; k++) {
  490.  
  491. for(int j=-1; j<=1; j++) {
  492.  
  493. int rgb = imgpixels[(y+k)*width+x+j];
  494.  
  495. int r = (rgb >> 16) & 0xff;
  496.  
  497. int g = (rgb >> 8) & 0xff;
  498.  
  499. int b = rgb & 0xff;
  500.  
  501. rs += r;
  502.  
  503. gs += g;
  504.  
  505. bs += b;
  506.  
  507. }
  508.  
  509. }
  510.  
  511.  
  512.  
  513. rs /= 9;
  514.  
  515. gs /= 9;
  516.  
  517. bs /= 9;
  518.  
  519.  
  520.  
  521. newimgpixels[y*width+x] = (0xff000000 |
  522.  
  523. rs << 16 | gs << 8 | bs);
  524.  
  525. }
  526.  
  527. }
  528.  
  529. }
  530.  
  531. }
  532.  
  533.  
  534. class Sharpen extends Convolver {
  535.  
  536.  
  537.  
  538. private final int clamp(int c) {
  539.  
  540. return (c > 255 ? 255 : (c < 0 ? 0 : c));
  541.  
  542. }
  543.  
  544.  
  545.  
  546. public void convolve() {
  547.  
  548. int r0=0, g0=0, b0=0;
  549.  
  550. for(int y=1; y<height-1; y++) {
  551.  
  552. for(int x=1; x<width-1; x++) {
  553.  
  554. int rs = 0;
  555.  
  556. int gs = 0;
  557.  
  558. int bs = 0;
  559.  
  560.  
  561.  
  562. for(int k=-1; k<=1; k++) {
  563.  
  564. for(int j=-1; j<=1; j++) {
  565.  
  566. int rgb = imgpixels[(y+k)*width+x+j];
  567.  
  568. int r = (rgb >> 16) & 0xff;
  569.  
  570. int g = (rgb >> 8) & 0xff;
  571.  
  572. int b = rgb & 0xff;
  573.  
  574. if (j == 0 && k == 0) {
  575.  
  576. r0 = r;
  577.  
  578. g0 = g;
  579.  
  580. b0 = b;
  581.  
  582. } else {
  583.  
  584. rs += r;
  585.  
  586. gs += g;
  587.  
  588. bs += b;
  589.  
  590. }
  591.  
  592. }
  593.  
  594. }
  595.  
  596.  
  597.  
  598. rs >>= 3;
  599.  
  600. gs >>= 3;
  601.  
  602. bs >>= 3;
  603.  
  604. newimgpixels[y*width+x] = (0xff000000 |
  605.  
  606. clamp(r0+r0-rs) << 16 |
  607.  
  608. clamp(g0+g0-gs) << 8 |
  609.  
  610. clamp(b0+b0-bs));
  611.  
  612. }
  613.  
  614. }
  615.  
  616. }
  617.  
  618. }


Ultima modifica effettuata da netarrow il 27/05/2007 alle 14:13
PM Quote
Avatar
elettrozp (Normal User)
Newbie


Messaggi: 9
Iscritto: 13/05/2007

Segnala al moderatore
Postato alle 13:56
Domenica, 27/05/2007
provo subito a capire il codice...
grazie netarrow:k:

PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 14:18
Domenica, 27/05/2007
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 il 27/05/2007 alle 14:19
PM Quote