I keep receiving either wierd offset of my cropped image or java.awt.image.RasterFormatException: (y + height) is outside of Raster exception. Any idea what I'm doing wrong?
I'm using scalling parameters: parameterX and parameterY, since I scale down imageView previously, but event without using those I don't receive the crop area selected by my rectangle
private void CropImage() {
BufferedImage src;
try {
src = ImageIO.read(imageSourceFile);
double parameterX = image.getWidth() / imageView.getFitWidth();
double parameterY = image.getHeight() / imageView.getFitHeight();
int positionX = (int) ((image.getWidth() * scrollPane.getHvalue() / parameterX) + croppingRectangle.getX());
int positionY = (int) ((image.getHeight() * scrollPane.getVvalue() / parameterY) + croppingRectangle.getY());
int desiredWidth = (int) (parameterX * croppingRectangle.getWidth());
int desiredHeight = (int) (parameterY * croppingRectangle.getHeight());
BufferedImage destBuffered = src.getSubimage(positionX, positionY, desiredWidth, desiredHeight);
File outputfile = new File("C:/Users/admin/Desktop/cropped.jpg");
ImageIO.write(destBuffered, "jpg", outputfile);
System.out.println("Image cropped successfully: " + outputfile.getPath());
} catch (IOException ex) {
System.out.println("couldnt load img");
}
}