General discussion

Locked

processing images using java

By sunrise ·
hi
i'd like to know if there is a way to modify an image on the interface using java code. Can i change dimenssions,change colors,count pixels using programmed code (not from the output window). Thanks a lot.
Regards,

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by gabriela2 In reply to processing images using j ...

Consider this:

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
//<applet code="PixT1.class" width="400" height="300" /applet>

public class PixT1 extends Applet{
Image mom1,mom2, mom3;
int width, height;

public void init(){
//setBackground(Color.black);
mom1=getImage(getDocumentBase( ),"mama.jpg");
width=400;
height=300;

try{
mom2=filteredImage(mom1);
}catch(InterruptedException ie){}

try{
mom3=filteredImage1(mom1);
}catch(InterruptedException ie){}

repaint();
}
public Image filteredImage(Image image)throws InterruptedException{
int[] pixels=new int[width*height];
PixelGrabber pg=new PixelGrabber(image,0,0,width,height,pixels,0,width);
int i=0;
if (pg.grabPixels() && ( ( pg.status() & ImageObserver.ALLBITS) !=0 ) ){

for(int y=0;y<height;y++) {
for(int x=0;x<width;x++){
i=y*width+x;

int a= (pixels & 0xff000000) >> 24;
int red= (pixels & 0x00ff0000) >> 16;
int green=(pixels & 0x0000ff00) >> 8;
int blue= (pixels & 0x000000ff);

green=(int) green/2;
pixels= (a<<24) | (red<<16) | (green<< | blue ;
}
}
}
image=createImage(new MemoryImageSource(width,height,pixels,0,width));
return image;
}


(to be continued)

Collapse -

by gabriela2 In reply to processing images using j ...

(next)

public Image filteredImage1(Image image)throws InterruptedException{
int[] pixels=new int[width*height];
PixelGrabber pg=new PixelGrabber(image,0,0,width,height,pixels,0,width);
int i=0;
if (pg.grabPixels() && ( ( pg.status() & ImageObserver.ALLBITS) !=0 ) ){

for(int y=0;y<height;y++) {
for(int x=0;x<width;x++){
i=y*width+x;

int a= (pixels & 0xff000000) >> 24;
int red= (pixels & 0x00ff0000) >> 16;
int green=(pixels & 0x0000ff00) >> 8;
int blue= (pixels & 0x000000ff);

red=(int) red/2;
green=(int) green/2;
blue=(int) blue/2;
pixels= (a<<24) | (red<<16) | (green<< | blue ;
}
}
}
image=createImage(new MemoryImageSource(width,height,pixels,0,width));
return image;
}

public void paint(Graphics g){
g.drawImage(mom1,20,20,400,300,this);
g.drawImage(mom2,20,340,400,300,this);
g.drawImage(mom3,420,340,400,300,this);
g.setColor(Color.white);
g.drawString( "mom1",20,20);
g.drawString( "mom2",20,340);
g.drawString( "mom3",420,340);
}
}

Put this java file in a folder, compile it and then see it( command prompt type appletviewer PixT1.java- if you have appletviewer tool installed, otherwise use <applet> tag in a html file.
In the same folder you have to have whatever jpg image(here is "mama.jpg")you have to test.
You'll see the same img but with filtered color.
Changing the size is not a problem but pixels manipulation is a little hard(for me at least).
Ofcourse the imports can be optimised.
Enjoy!

Collapse -

by gabriela2 In reply to processing images using j ...

ok,ok, my jpg file is 400x300, so you have to modify width and hight to 820x620(860*660 better, I guess) in init() method and in <applet> tag, otherwise you'll not see all three images,
But you have to set the right dimensions for your own image so that they can have enough room to be visible in the applet.

Collapse -

by gabriela2 In reply to processing images using j ...

Hellooo...Anybody there?
Does this work for you?
When I worked with java graphics I had kind of fever, I couldn't get out of computer.

Collapse -

by zilaq In reply to processing images using j ...

I work for my own small software company and I posted on the company website some older code. I think you will find it useful. Here's the link:

http://www.bitsp.com/

And here are the links for image manipulation samples:

http://www.bitsp.com/?module_category/action_content/cid_126

and

http://www.bitsp.com/?module_category/action_content/cid_127

The programs implement various algorithms for resize, rotation, contrast, binarization, boolean operations, front detection and image segmentation. The full source is available in the archives.

Collapse -

by superdisco In reply to processing images using j ...

I am not sure if you are still having problems with image processing in java, but if you are you should have a look at ImageJ which accepts java written macros etc and has inbuilt image manipulation within it such as thresholding, edge detection etc... see the manual for more info on how you can use it.

You can find it here:
http://rsb.info.nih.gov/ij/

and it is free :-)

cheers

Collapse -

by mySiti In reply to processing images using j ...

Hi

I try to run the program but have errors.

JAI\PixT1.java:55: operator & cannot be applied to int[],int
int a= (pixels & 0xff000000) >> 24;
^
JAI\PixT1.java:56: operator & cannot be applied to int[],int
int red= (pixels & 0x00ff0000) >> 16;
^
JAI\PixT1.java:57: operator & cannot be applied to int[],int
int green=(pixels & 0x0000ff00) >> 8;
^
JAI\PixT1.java:58: operator & cannot be applied to int[],int
int blue= (pixels & 0x000000ff);
^
JAI\PixT1.java:61: incompatible types
found : int
required: int[]
pixels= (a<<24) | (red<<16) | (green<< | blue ;
^
PixT1.java:87: operator & cannot be applied to int[],int
int a= (pixels & 0xff000000) >> 24;
^
JAI\PixT1.java:88: operator & cannot be applied to int[],int
int red= (pixels & 0x00ff0000) >> 16;
^
java:89: operator & cannot be applied to int[],int
int green=(pixels & 0x0000ff00) >> 8;
^
java:90: operator & cannot be applied to int[],int
int blue= (pixels & 0x000000ff);
^
JAI\PixT1.java:95: incompatible types
found : int
required: int[]
pixels= (a<<24) | (red<<16) | (green<< | blue ;
^
10 errors
Process completed.

Is there any solutions to it?
I'm new in this area and using J2SE 1.4.
Thanks for any idea.

Siti

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums