使用 Java 查找 PDF 中的 CMYK 覆盖范围
我正在使用一个工具,用户可以在其中使用图像上传PDF,然后从图像中接收有关区域和CMYK颜色的信息。
最初,我认为RGB值就足够了,因此我使用PDFBox库(PDFrenderer类)将PDF转换为RGB映像,然后计算每个像素的颜色。这有效,但是现在我需要CMYK值。我花了一些时间尝试将RGB值转换为CMYK值,但无法获得良好的结果。这根本不准确。我以前从未使用过颜色空间,所以这是一场挣扎。
是否有可靠的方式来加载PDF文档并读取其CMYK值?我错过了将RGB转换为CMYK的可靠方法吗?
// Load CMYK ICC file
InputStream is = TestClass.class.getClassLoader().getResourceAsStream("USWebCoatedSWOP.icc")
// Load PDF Document
PDDocument doc = PDDPDDocument.load(TestClass.class.getClassLoader().getResourceAsStream("test.pdf"))
// Render image from document
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 600, ImageType.RGB);
// Read a pixel from image
float[] rgbF = new float[3];
bufferedImage.getData().getPixel(0,0,rgbF);
// Load color spaces
ColorSpace cmykCS = new ICC_ColorSpace(ICC_Profile.getInstance(is));
// Prepare RGB values for CMYK (I assume we want RGB values in [0.0, 1.0] ranger. I did try [0.0, 255.0] range as well. Was even worse).
for (int i = 0; i < 3; i++) {
rgbF[i] = rgbF[i] / 255.0f;
}
// Convert to CMYK
float[] cmyk = cmykCS.fromRGB(rgbF);
我还尝试首先转换为Ciexyz,但结果相同:
ColorSpace rgbCS = ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB);
float[] cmyk = ascmykCS.fromCIEXYZ(rgbCS.toCIEXYZ(rgbF))
I'm working on a tool where the user could upload a PDF with an image and then receive information about area and CMYK colors from the image.
Initially I thought that RGB values would be enough, so I used PdfBox library(PDFRenderer class) to convert the PDF to an RGB image and then counted the colors for each pixel. This works, but now I need CMYK values. I've been spending some time trying to convert RGB values to CMYK values, but can't get good results. It's not accurate at all. I've never worked with color spaces before, so it's a struggle.
Is there a reliable way of loading a PDF document and reading its CMYK values? Is there maybe a reliable way of converting RGB to CMYK, that I missed?
// Load CMYK ICC file
InputStream is = TestClass.class.getClassLoader().getResourceAsStream("USWebCoatedSWOP.icc")
// Load PDF Document
PDDocument doc = PDDPDDocument.load(TestClass.class.getClassLoader().getResourceAsStream("test.pdf"))
// Render image from document
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 600, ImageType.RGB);
// Read a pixel from image
float[] rgbF = new float[3];
bufferedImage.getData().getPixel(0,0,rgbF);
// Load color spaces
ColorSpace cmykCS = new ICC_ColorSpace(ICC_Profile.getInstance(is));
// Prepare RGB values for CMYK (I assume we want RGB values in [0.0, 1.0] ranger. I did try [0.0, 255.0] range as well. Was even worse).
for (int i = 0; i < 3; i++) {
rgbF[i] = rgbF[i] / 255.0f;
}
// Convert to CMYK
float[] cmyk = cmykCS.fromRGB(rgbF);
I also tried first converting to CIEXYZ, but same result:
ColorSpace rgbCS = ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB);
float[] cmyk = ascmykCS.fromCIEXYZ(rgbCS.toCIEXYZ(rgbF))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论