在此处输入图像描述 - 图像的原始位置
在此处输入图像描述- 放大图像
如何使文本和文字可读、清晰、高质量的图像?
源代码:
private Pane createPagePane(int pageIndex, PDDocument document, PDFRenderer renderer, AnchorPane anchorPane) throws IOException {
PDPage page = document.getPage(pageIndex);
PDRectangle pageSize = page.getMediaBox();
boolean isLandscape = pageSize.getWidth() > pageSize.getHeight();
double widthToFit = anchorPane.getPrefWidth();
double heightToFit = anchorPane.getPrefHeight();
double scaleFactor = isLandscape ? Math.min(widthToFit / pageSize.getWidth(), heightToFit / pageSize.getHeight())
: Math.min(heightToFit / pageSize.getWidth(), widthToFit / pageSize.getHeight());
int targetWidth = (int) (pageSize.getWidth() * scaleFactor);
int targetHeight = (int) (pageSize.getHeight() * scaleFactor);
BufferedImage bufferedImage = renderer.renderImage(pageIndex, (float) scaleFactor, ImageType.RGB);
BufferedImage backgroundImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = backgroundImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setColor(java.awt.Color.WHITE);
g2d.fillRect(0, 0, targetWidth, targetHeight);
g2d.drawImage(bufferedImage, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
Image fxImage = SwingFXUtils.toFXImage(backgroundImage, null);
ImageView imageView = new ImageView(fxImage);
imageView.setFitWidth(targetWidth);
imageView.setFitHeight(targetHeight);
imageView.setPreserveRatio(true);
imageView.setSmooth(true);
imageView.setCache(true);
Pane pagePane = new Pane(imageView);
pagePane.setPrefSize(targetWidth, targetHeight);
pagePane.setOnMouseClicked(event -> {
pagePane.getStyleClass().add("selected-pane");
});
rectMouseInteraction(pagePane, document, pageIndex, scaleFactor);
return pagePane;
}
我期待的是这样的结果:
在此处输入图像描述 - 这也是通过缓冲图像转换和创建的。
即使我放大图像,我也希望我的也能有这样的质量。
如果您的面板仅包含文本,则应使用矢量字体而不是图像。