本文整理汇总了Java中javax.imageio.ImageWriteParam类的典型用法代码示例。如果您正苦于以下问题:Java ImageWriteParam类的具体用法?Java ImageWriteParam怎么用?Java ImageWriteParam使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ImageWriteParam类属于javax.imageio包,在下文中一共展示了ImageWriteParam类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveImage
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
private static long saveImage(final BufferedImage image,
final ImageWriter writer,
final ImageWriteParam writerParams,
final String mode,
final String suffix) throws IOException
{
final File imgFile = new File("WriterCompressionTest-"
+ mode + '.' + suffix);
System.out.println("Writing file: " + imgFile.getAbsolutePath());
final ImageOutputStream imgOutStream
= ImageIO.createImageOutputStream(new FileOutputStream(imgFile));
try {
writer.setOutput(imgOutStream);
writer.write(null, new IIOImage(image, null, null), writerParams);
} finally {
imgOutStream.close();
}
return imgFile.length();
}
示例2: writeTest
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public byte[] writeTest(BufferedImage bi,
ImageWriteParam p,
IIOMetadata m) throws IOException {
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
// write test image as jpeg
ImageOutputStream ios =
ImageIO.createImageOutputStream(baos);
w.setOutput(ios);
w.write(null,
new IIOImage(bi, null, m),
p);
ios.close();
return baos.toByteArray();
}
示例3: writeJPG
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public static void writeJPG( BufferedImage rendered, float quality, File f ) {
try {
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam( null );
jpegParams.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
jpegParams.setCompressionQuality( 1f );
ImageWriter writer = ImageIO.getImageWritersByFormatName( "jpeg" ).next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); // Needed see javadoc
param.setCompressionQuality( quality ); // Highest quality
writer.setOutput( new FileImageOutputStream( f ) );
writer.write( null, new IIOImage( rendered, null, null ), jpegParams );
} catch ( Throwable th ) {
th.printStackTrace();
}
}
示例4: jfifOK
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
private boolean jfifOK(ImageTypeSpecifier imageType,
ImageWriteParam param,
IIOMetadata streamMetadata,
IIOMetadata imageMetadata) {
// If the image type and metadata are JFIF compatible, return true
if ((imageType != null) &&
(!JPEG.isJFIFcompliant(imageType, true))) {
return false;
}
if (imageMetadata != null) {
JPEGMetadata metadata = null;
if (imageMetadata instanceof JPEGMetadata) {
metadata = (JPEGMetadata) imageMetadata;
} else {
metadata = (JPEGMetadata)convertImageMetadata(imageMetadata,
imageType,
param);
}
// metadata must have a jfif node
if (metadata.findMarkerSegment
(JFIFMarkerSegment.class, true) == null){
return false;
}
}
return true;
}
示例5: encodeRLE8Test
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
private static void encodeRLE8Test() throws IOException {
// create 8bpp image
byte[] r = new byte[256];
r[0] = (byte)0xff;
byte[] g = new byte[256];
g[1] = (byte)0xff;
byte[] b = new byte[256];
b[2] = (byte)0xff;
IndexColorModel icm = new IndexColorModel(8, 256, r, g, b);
BufferedImage bimg = new BufferedImage(100, 100,
BufferedImage.TYPE_BYTE_INDEXED,
icm);
Graphics gr = bimg.getGraphics();
gr.setColor(Color.green);
gr.fillRect(0, 0, 100, 100);
doTest(bimg, "BI_RLE8", ImageWriteParam.MODE_EXPLICIT);
}
示例6: doTest
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
private static void doTest(int mode) {
String fileFormat = "bmp";
try {
ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix(fileFormat).next();
if(iw == null) {
throw new RuntimeException("No available image writer for "
+ fileFormat
+ " Test failed.");
}
File file = new File("image." + fileFormat);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
BufferedImage bimg = new BufferedImage(100,
100, BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.getGraphics();
g.setColor(Color.green);
g.fillRect(0,0,100,100);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(mode);
IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg),
param);
IIOImage iioImg = new IIOImage(bimg, null, meta);
iw.write(null, iioImg, param);
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("Test failed.");
}
}
示例7: encode
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
/**
* Encodes an image in JPEG format and writes it to an output stream.
*
* @param bufferedImage the image to be encoded (<code>null</code> not
* permitted).
* @param outputStream the OutputStream to write the encoded image to
* (<code>null</code> not permitted).
*
* @throws IOException if there is an I/O problem.
* @throws NullPointerException if <code>bufferedImage</code> is
* <code>null</code>.
*/
public void encode(BufferedImage bufferedImage, OutputStream outputStream)
throws IOException {
if (bufferedImage == null) {
throw new IllegalArgumentException("Null 'image' argument.");
}
if (outputStream == null) {
throw new IllegalArgumentException("Null 'outputStream' argument.");
}
Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) iterator.next();
ImageWriteParam p = writer.getDefaultWriteParam();
p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
p.setCompressionQuality(this.quality);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
writer.setOutput(ios);
writer.write(null, new IIOImage(bufferedImage, null, null), p);
ios.flush();
writer.dispose();
ios.close();
}
示例8: compressSave
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public void compressSave(MultipartFile file, String uploadLocation) throws IOException {
//Compresses JPG file but kills the quality.
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
File compressedImageFile = new File(uploadLocation);
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if(param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
}
writer.write(null, new IIOImage(image, null, null), param);
}
示例9: write
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public void write(IIOMetadata sm,
IIOImage iioimage,
ImageWriteParam p) throws IOException {
if (stream == null) {
throw new IllegalStateException("output == null!");
}
if (iioimage == null) {
throw new IllegalArgumentException("iioimage == null!");
}
if (iioimage.hasRaster()) {
throw new UnsupportedOperationException("canWriteRasters() == false!");
}
resetLocal();
GIFWritableStreamMetadata streamMetadata;
if (sm == null) {
streamMetadata =
(GIFWritableStreamMetadata)getDefaultStreamMetadata(p);
} else {
streamMetadata =
(GIFWritableStreamMetadata)convertStreamMetadata(sm, p);
}
write(true, true, streamMetadata, iioimage, p);
}
示例10: writeToSequence
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public void writeToSequence(IIOImage image, ImageWriteParam param)
throws IOException {
if (stream == null) {
throw new IllegalStateException("output == null!");
}
if (image == null) {
throw new IllegalArgumentException("image == null!");
}
if (image.hasRaster()) {
throw new UnsupportedOperationException("canWriteRasters() == false!");
}
if (!isWritingSequence) {
throw new IllegalStateException("prepareWriteSequence() was not invoked!");
}
write(!wroteSequenceHeader, false, theStreamMetadata,
image, param);
if (!wroteSequenceHeader) {
wroteSequenceHeader = true;
}
this.imageIndex++;
}
示例11: writeImageWithHist
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
protected File writeImageWithHist(BufferedImage bi) throws IOException {
File f = File.createTempFile("hist_", ".png", new File("."));
ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
ImageTypeSpecifier type = new ImageTypeSpecifier(bi);
IIOMetadata imgMetadata = writer.getDefaultImageMetadata(type, param);
/* add hIST node to image metadata */
imgMetadata = upgradeMetadata(imgMetadata, bi);
IIOImage iio_img = new IIOImage(bi,
null, // no thumbnails
imgMetadata);
writer.write(iio_img);
ios.flush();
ios.close();
return f;
}
示例12: createTestImage
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
private static Image createTestImage() throws IOException {
BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
ImageWriteParam param = writer.getDefaultWriteParam();
writer.setOutput(ios);
writer.prepareWriteSequence(null);
writer.writeToSequence(new IIOImage(frame1, null, null), param);
writer.writeToSequence(new IIOImage(frame2, null, null), param);
writer.endWriteSequence();
writer.reset();
writer.dispose();
ios.flush();
ios.close();
return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
示例13: TIFFDeflater
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public TIFFDeflater(String compressionType,
int compressionTagValue,
ImageWriteParam param,
int predictorValue) {
super(compressionType, compressionTagValue, true);
this.predictor = predictorValue;
// Set the deflate level.
int deflateLevel;
if(param != null &&
param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
float quality = param.getCompressionQuality();
deflateLevel = (int)(1 + 8*quality);
} else {
deflateLevel = Deflater.DEFAULT_COMPRESSION;
}
this.deflater = new Deflater(deflateLevel);
}
示例14: compress
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
/**
* 根据质量压缩图片,不改变图片的尺寸
*
* @param imgContent
* 图片内容
* @param quality
* 缩放比例
* @param extName
* 后缀名
* @return
*/
public byte[] compress(byte[] imgContent, float quality, String extName) {
if (quality > 1 || quality <= 0 || imgContent == null || WakaUtils.string.isEmpty(extName)) {
throw new ZhhrUtilException("压缩文件失败!入参为空");
}
try (InputStream is = new ByteArrayInputStream(imgContent);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);) {
BufferedImage image = ImageIO.read(is);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(extName);
ImageWriter writer = writers.next();
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.write(null, new IIOImage(image, null, null), param);
writer.dispose();
return os.toByteArray();
} catch (IOException e) {
throw new ZhhrUtilException(e.getMessage(), e);
}
}
示例15: main
import javax.imageio.ImageWriteParam; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
Iterator witer = ImageIO.getImageWritersByFormatName("png");
ImageWriter w = (ImageWriter)witer.next();
File f = File.createTempFile("WriteProgressive", ".png");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
w.setOutput(ios);
BufferedImage bi = new BufferedImage(100, 100,
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = bi.createGraphics();
Random r = new Random(10);
for (int i = 0; i < 10000; i++) {
Color c =
new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
g.setColor(c);
g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);
}
IIOImage iioimage = new IIOImage(bi, null, null);
ImageWriteParam param = w.getDefaultWriteParam();
param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
try {
w.write(null, iioimage, param);
} catch (NullPointerException npe) {
throw new RuntimeException("Got NPE during write!");
}
ios.close();
BufferedImage bi2 = ImageIO.read(f);
f.delete();
ImageCompare.compare(bi, bi2);
}