1

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");
    }

}
SDamnation
  • 41
  • 3
  • Did you try printing out parameterX, parameterY, positionX, positionY, desiredWidth, and desiredHeight, to see if their values look appropriate? – VGR Oct 17 '19 at 21:30
  • Sure, but even if the printed values are correct, there's still some offset in the output file – SDamnation Oct 17 '19 at 22:12
  • Multiplying the width/height by the scrollbar position seems like it’s probably wrong. If the vertical scrollbar is at the bottom, you don’t want positionY to start at the image’s bottom edge. Perhaps you should use the [viewport bounds](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/ScrollPane.html#getViewportBounds%28%29) in your calculations instead of the scrollbar values? – VGR Oct 18 '19 at 00:57

0 Answers0