을하는 데 도움이
희망 나는 자바 ImageIO에서 라이브러리 (https://jai-imageio.dev.java.net)를 사용합니다. 그들은 완벽하지는 않지만 간단하고 일을 끝낼 수 있습니다. 지금까지 CMYK에서 RGB로 변환하는 방법을 생각해 볼 때 가장 좋은 방법은 다음과 같습니다.
다운로드 및 플랫폼에 대한 ImageIO에서 항아리와 네이티브 라이브러리를 설치합니다. 기본 라이브러리는 필수적입니다. ImageIO JAR 파일이 없으면 CMYK 이미지를 감지 할 수 없습니다. 원래 필자는 네이티브 라이브러리가 성능을 향상시킬 수는 있지만 모든 기능에 필요하지는 않다는 인상하에있었습니다. 내가 틀렸어. 내가 눈치
유일하게 다른 것은
변환 된 RGB 이미지는 때로는 CMYK 이미지보다 훨씬 가볍다는 것이다. 누군가가 그 문제를 해결하는 방법을 안다면, 나는 감사하게 여길 것이다. 다음은
는 지원되는 형식의 RGB 이미지로 CMYK 이미지를 변환하는 몇 가지 코드입니다.
, 감사합니다
랜디 Stegbauer
package cmyk;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.lang.StringUtils;
public class Main
{
/**
* Creates new RGB images from all the CMYK images passed
* in on the command line.
* The new filename generated is, for example "GIF_original_filename.gif".
*
*/
public static void main(String[] args)
{
for (int ii = 0; ii < args.length; ii++)
{
String filename = args[ii];
boolean cmyk = isCMYK(filename);
System.out.println(cmyk + ": " + filename);
if (cmyk)
{
try
{
String rgbFile = cmyk2rgb(filename);
System.out.println(isCMYK(rgbFile) + ": " + rgbFile);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
/**
* If 'filename' is a CMYK file, then convert the image into RGB,
* store it into a JPEG file, and return the new filename.
*
* @param filename
*/
private static String cmyk2rgb(String filename) throws IOException
{
// Change this format into any ImageIO supported format.
String format = "gif";
File imageFile = new File(filename);
String rgbFilename = filename;
BufferedImage image = ImageIO.read(imageFile);
if (image != null)
{
int colorSpaceType = image.getColorModel().getColorSpace().getType();
if (colorSpaceType == ColorSpace.TYPE_CMYK)
{
BufferedImage rgbImage =
new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(image, rgbImage);
rgbFilename = changeExtension(imageFile.getName(), format);
rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();
ImageIO.write(rgbImage, format, new File(rgbFilename));
}
}
return rgbFilename;
}
/**
* Change the extension of 'filename' to 'newExtension'.
*
* @param filename
* @param newExtension
* @return filename with new extension
*/
private static String changeExtension(String filename, String newExtension)
{
String result = filename;
if (filename != null && newExtension != null && newExtension.length() != 0);
{
int dot = filename.lastIndexOf('.');
if (dot != -1)
{
result = filename.substring(0, dot) + '.' + newExtension;
}
}
return result;
}
private static boolean isCMYK(String filename)
{
boolean result = false;
BufferedImage img = null;
try
{
img = ImageIO.read(new File(filename));
}
catch (IOException e)
{
System.out.println(e.getMessage() + ": " + filename);
}
if (img != null)
{
int colorSpaceType = img.getColorModel().getColorSpace().getType();
result = colorSpaceType == ColorSpace.TYPE_CMYK;
}
return result;
}
}
이 예제에는 0에서 255^2 사이의 값이 있습니다. 따라서 올바른 전환의 예가 아닙니다. –