It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Problem with using getSubimage(x,y,w,h) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with using getSubimage(x,y,w,h)" Watch "Problem with using getSubimage(x,y,w,h)" New topic
Author

Problem with using getSubimage(x,y,w,h)

Nikhil Khandave
Greenhorn

Joined: Mar 02, 2010
Posts: 1
Hi all,
i m getting problem using getSubimage(), the task that i have to performed is to find the first pixel from 4 sides i.e top, bottom,left,right
the code is as follow

package practiceexample;


import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.io.*;
import java.awt.Color;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageConsumer;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.awt.image.WritableRaster;

public class scanPixel extends JFrame{

private BufferedImage bimage;
private BufferedImage my_bimage;
private ImageIO imageio;
private File filename;
private int width;
private int height;
private static AffineTransform transform;
private ColorModel cm;
private int startx = 0;
private int starty = 0;
private int new_width = 0;
private int new_height = 0;
private BufferedImage bimage_top;
String fileurl = "//home//nis//batch2008//knikhil//NetBeansProjects//PracticeExample1//src//practiceexample//cannyLeaf.jpg";
public scanPixel() {
filename = new File(fileurl);
System.out.println("filename = "+filename);
try {
bimage = imageio.read(filename);
}
catch(IOException e) {
System.out.println("["+e+"]");
}
width = bimage.getWidth();
height = bimage.getHeight();
my_bimage = scanningPixel(bimage);

System.out.println(my_bimage.getWidth()+"---"+my_bimage.getHeight());
setBackground(Color.WHITE);
setVisible(true);
setSize(new_width,new_height);

}
public void paint(Graphics g) {
my_bimage.createGraphics();
g.drawImage(my_bimage, 0, 0,null);
}
public BufferedImage scanningPixel(BufferedImage bimage) {

scanningImageForTop(bimage);
scanningImageForRight(bimage);
scanningImageForBottom(bimage);
scanningImageForLeft(bimage);

bimage_top = resizeImage(bimage);

return bimage_top;
}
public void scanningImageForTop(BufferedImage bimage) {

width = bimage.getWidth();
height = bimage.getHeight();

int i,j;
int flag = 0;

for(i = 0; i < height; ++i) {
for(j = 0; j < width; ++j) {
int p = bimage.getRGB( j, i);
if(p == -1) {
starty = i;
flag = 1;
break;
}
}
if(flag == 1)
break;
}
}
public void scanningImageForRight(BufferedImage bimage) {

int flag =0;

width = bimage.getWidth();
height = bimage.getHeight();

for(int i = (width-1); i >= 0; --i) {
for(int j = 0; j < height; ++j) {
int p = bimage.getRGB(i, j);
if(p == -1) {
new_width = i;
flag = 1;
break;
}
}
if(flag == 1)
break;
}

}
public void scanningImageForBottom(BufferedImage bimage) {
BufferedImage b_bottom = null;
int flag =0;
width = bimage.getWidth();
height = bimage.getHeight();
System.out.println("Width = "+width+"--Height = "+height);
for(int i = height-1; i >= 0; --i) {
for(int j = width-1; j >= 0; --j) {

int p = bimage.getRGB(j,i);
if(p == -1) {
new_height = i;
flag = 1;
break;
}
}
if(flag == 1)
break;
}

}
public void scanningImageForLeft(BufferedImage bimage) {
BufferedImage b_right = null;
int flag =0;
width = bimage.getWidth();
height = bimage.getHeight();
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {

int p = bimage.getRGB(i, j);
if(p == -1) {
startx = i;
flag = 1;
break;
}
}
if(flag == 1)
break;

}
}
public BufferedImage resizeImage(BufferedImage bimage) {
BufferedImage sub_image, new_image;
System.out.println("StartX = "+startx);
System.out.println("StartY = "+starty);
System.out.println("Width = "+new_width);
System.out.println("Height = "+new_height);


new_image = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_ARGB);

new_width -= startx;
new_height-=(starty);

sub_image = bimage.getSubimage(startx, starty, new_width, new_height);


WritableRaster wr = sub_image.copyData (null) ;
new_image.setData ( wr ) ;

return new_image;
}
public static void main(String args[]) {
scanPixel sp = new scanPixel();

}
}

if anyone knows the solution please rply asap.
thanks in advance.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
most forums have a code button that when clicked produce some code tags that you paste your code between,
or, paste your code, highlight it, then click the code button.

try editing your post (edit button), to add code tags so we can read it in a format that is easy on our eyes

e.g. this is much easier on the eyes



than this

class FirstProgram{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
Hi,
I am not sure if this help; I went through your code quickly and I realized based on your question below

i m getting problem using getSubimage(), the task that i have to performed is to find the first pixel from 4 sides i.e top, bottom,left,right


So you have the new BufferedImage from the method getSubimage(). So what make a problem if the getRBG(x,y) if x and y are the coordinate point on the image
so the top left corner has a point (0,0) and the top right corner has (subImage.getWidth(),0) etc..
I am not sure if this is correct answer but it may help

I have looked at the API doc for hte getRBG(x,y) method which return the pixel at specific point

Have you looked at the method public int[] getRGB(int startX, int startY, int w, int h,
int[] rgbArray, int offset, int scansize) in the BufferedImage



Regards,
Alan Mehio
London,UK


Regards,
Alan Mehio
London, UK
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with using getSubimage(x,y,w,h)