Created
June 28, 2016 15:26
-
-
Save RolandColored/1f98a7160b19f4fa5a13a91f8af21c31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private BufferedImage readImageWithColorSpacePreservation(byte[] imageData) { | |
try (ImageInputStream inputStream = ImageIO.createImageInputStream(new ByteArrayInputStream(imageData))) { | |
// Get the reader | |
Iterator<ImageReader> readers = ImageIO.getImageReaders(inputStream); | |
if (!readers.hasNext()) { | |
throw new IllegalArgumentException("No reader found"); | |
} | |
ImageReader reader = readers.next(); | |
try { | |
reader.setInput(inputStream); | |
Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); | |
// determine if CMYK decoding is possible | |
ImageTypeSpecifier cmykType = null; | |
while (types.hasNext()) { | |
ImageTypeSpecifier type = types.next(); | |
int csType = type.getColorModel().getColorSpace().getType(); | |
if (csType == ColorSpace.TYPE_CMYK) { | |
cmykType = type; | |
break; | |
} | |
} | |
// set reader to preserve CMYK color space if found | |
ImageReadParam param = reader.getDefaultReadParam(); | |
if (cmykType != null) { | |
param.setDestinationType(cmykType); | |
} | |
// Finally read the image, using settings from param | |
return reader.read(0, param); | |
} | |
finally { | |
reader.dispose(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment