本文整理汇总了Java中ar.com.hjg.pngj.PngWriter.setCompLevel方法的典型用法代码示例。如果您正苦于以下问题:Java PngWriter.setCompLevel方法的具体用法?Java PngWriter.setCompLevel怎么用?Java PngWriter.setCompLevel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ar.com.hjg.pngj.PngWriter
的用法示例。
在下文中一共展示了PngWriter.setCompLevel方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writePngImage
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
/**
* Writes a {@link BufferedImage} to the specified {@link OutputStream} using the PNGJ library
* which is much faster than Java's ImageIO library.
*
* This implementation was copied from
* <a href="https://github.com/leonbloy/pngj/wiki/Snippets">
* https://github.com/leonbloy/pngj/wiki/Snippets
* </a>.
*
* @param bufferedImage image to write.
* @param compressionLevel 0 (no compression) - 9 (max compression)
* @param filterType internal prediction filter type.
* @param outputStream target stream.
*
* @throws IOException
* if the image is not ARGB or it's data buffer contains the wrong number of banks.
*/
public static void writePngImage(final BufferedImage bufferedImage,
final int compressionLevel,
final FilterType filterType,
final OutputStream outputStream)
throws IOException {
if (bufferedImage.getType() != BufferedImage.TYPE_INT_ARGB) {
throw new IOException("invalid image type (" + bufferedImage.getType() +
"), must be BufferedImage.TYPE_INT_ARGB");
}
final ImageInfo imageInfo = new ImageInfo(bufferedImage.getWidth(), bufferedImage.getHeight(), 8, true);
final PngWriter pngWriter = new PngWriter(outputStream, imageInfo);
pngWriter.setCompLevel(compressionLevel);
pngWriter.setFilterType(filterType);
final DataBufferInt dataBuffer =((DataBufferInt) bufferedImage.getRaster().getDataBuffer());
if (dataBuffer.getNumBanks() != 1) {
throw new IOException("invalid number of banks (" + dataBuffer.getNumBanks() + "), must be 1");
}
final SinglePixelPackedSampleModel sampleModel = (SinglePixelPackedSampleModel) bufferedImage.getSampleModel();
final ImageLineInt line = new ImageLineInt(imageInfo);
final int[] data = dataBuffer.getData();
for (int row = 0; row < imageInfo.rows; row++) {
int elem = sampleModel.getOffset(0, row);
for (int col = 0; col < imageInfo.cols; col++) {
final int sample = data[elem++];
ImageLineHelper.setPixelRGBA8(line, col, sample);
}
pngWriter.writeRow(line, row);
}
pngWriter.end();
// // This looked like a nicer option, but only works for DataBufferByte (not DataBufferInt)
// final ImageLineSetARGBbi lines = new ImageLineSetARGBbi(bufferedImage, imageInfo);
// pngWriter.writeRows(lines);
// pngWriter.end();
}
示例2: timePNGJEncode
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
@Test
public void timePNGJEncode() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
boolean grayscale = !indexed && colorModel.getNumColorComponents() == 1;
ImageInfo ii = new ImageInfo(image.getWidth(), image.getHeight(), 8, hasAlpha, grayscale,
indexed);
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(4);
// pw.setDeflaterStrategy(Deflater.NO_COMPRESSION);
pw.setFilterType(ar.com.hjg.pngj.FilterType.getByVal(filterType.getType()));
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getNumComponents();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
palette.setEntry(i, icm.getRed(i), icm.getGreen(i), icm.getBlue(i));
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
alpha[i] = icm.getAlpha(i);
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] png = bos.toByteArray();
collectPng("pngj", png);
}
示例3: timePNGJEncode
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
public void timePNGJEncode(ar.com.hjg.pngj.FilterType filterType, String name) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
boolean grayscale = !indexed && colorModel.getNumColorComponents() == 1;
ImageInfo ii = new ImageInfo(image.getWidth(), image.getHeight(), 8, hasAlpha, grayscale,
indexed);
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(compression);
pw.setFilterType(filterType);
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getNumComponents();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
palette.setEntry(i, icm.getRed(i), icm.getGreen(i), icm.getBlue(i));
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
alpha[i] = icm.getAlpha(i);
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] png = bos.toByteArray();
collectPng(name, png);
}
示例4: roundTripPNGJ
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
private void roundTripPNGJ(BufferedImage original, RenderedImage source) throws IOException {
ColorModel colorModel = source.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(source);
int numColorComponents = colorModel.getNumColorComponents();
boolean grayscale = !indexed && numColorComponents < 3;
byte bitDepth = scanlines.getBitDepth();
ImageInfo ii = new ImageInfo(source.getWidth(), source.getHeight(), bitDepth, hasAlpha, grayscale,
indexed);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(4);
pw.setFilterType(ar.com.hjg.pngj.FilterType.FILTER_NONE);
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getMapSize();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
final int red = icm.getRed(i);
final int green = icm.getGreen(i);
final int blue = icm.getBlue(i);
palette.setEntry(i, red, green, blue);
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
final int a = icm.getAlpha(i);
alpha[i] = a;
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
for (int row = 0; row < source.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] bytes = bos.toByteArray();
writeToFile(new File("./target/roundTripNone", sourceFile.getName()), bytes);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(bis);
assertEquals(original.getWidth(), image.getWidth());
assertEquals(original.getHeight(), image.getHeight());
// we cannot match in output the image types generated by the CLIB reader, they are not
// really matching the PNG data model
if(!clibAvailable) {
assertEquals(original.getSampleModel(), image.getSampleModel());
assertEquals(original.getColorModel(), image.getColorModel());
}
for (int x = 0; x < original.getWidth(); x++) {
for (int y = 0; y < original.getHeight(); y++) {
int rgbOriginal = original.getRGB(x, y);
int rgbActual = image.getRGB(x, y);
assertEquals("Comparison failed at " + x + "," + y, rgbOriginal, rgbActual);
}
}
}
示例5: timePNGJEncode
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
@Test
public void timePNGJEncode() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
boolean grayscale = !indexed && colorModel.getNumColorComponents() == 1;
ImageInfo ii = new ImageInfo(image.getWidth(), image.getHeight(), 8, hasAlpha, grayscale,
indexed);
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(compression);
// pw.setDeflaterStrategy(Deflater.NO_COMPRESSION);
pw.setFilterType(filterType);
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getNumComponents();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
palette.setEntry(i, icm.getRed(i), icm.getGreen(i), icm.getBlue(i));
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
alpha[i] = icm.getAlpha(i);
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] png = bos.toByteArray();
collectPng("pngj", png);
}