I'm trying to crop an image according to a dynamically drawn square. In order to get exactly the drawn square cropped, I (before cropping) resize my image to fit the panel width & height, so that every rectangle drawn on the panel should coresponed exactly to the same square's points value on physical image.
First, Is there a problem in this implementation way? if no, so despite this resizing step, the resulted cropped rectangle is always "moved" few (pixels? inches?)right.
I think since the commnd "resize" of image magick I use works with pixels, and the dragged square-with screen Points value, it causes the problem. any ideas of how doing the convertion between them? I'll be more than thankfull though I've really tried hard sloving it with different ways.
here is relevant code:
public void resizeImage(int panelW){
int percent;
String sign;
float imgW=img.getWidth(null);
percent=(int) (((panelW-imgW)/imgW)*100);
sign="+resize";
if(panelW <imgW){
percent=(int)(((imgW-panelW)/panelW)*100);
sign="-resize";
}
String command="convert "+imagePath+" "+sign+" "+percent+"% "+imagePath;
//...executeCommand(command) and repaint();
}
I've also tried resizing the image before cropping with this:
Dimension newImagesize=getScaledDimension(); String command="convert"+imagePath+"-resize"+newImagesize.width+"X"+newImagesize.height+" "+imagePath
where getScaledDimension() is taken from here: Java image resize, maintain aspect ratio
and then I just upload the resized image, which is supposed to have the panel's dimension like this:
img= new ImageIcon(imgPath).getImage();
repaint();
As I said, the cropped image is always some pixels(?) right moved, so I asume there is a problem in the resizeImage() measurments i do but it seams logic to me..
the crop function uses resizer:
Rectangle r=resizer.getBounds();
try {
String x="",y="";
if(r.x>0)x="+"+r.x;
if(r.y>0)y="+"+r.y;
cropImage(r.width, r.height, x, y);
and uses the crop ImageMagick comand.