如何在滚动窗格内正确裁剪?

时间:2019-10-17 20:28:30

标签: java javafx scrollpane

我一直收到裁剪图像的异常偏移量或java.awt.image.RasterFormatException:(y + height)在Raster异常之外。知道我在做什么错吗?

我正在使用缩放参数:parameterX和parameterY,因为我以前缩小过imageView,但是没有使用这些事件,我没有收到矩形所选择的裁剪区域

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

}

0 个答案:

没有答案