当前位置: 首页>>代码示例>>Java>>正文


Java ImageWriter.getDefaultWriteParam方法代码示例

本文整理汇总了Java中javax.imageio.ImageWriter.getDefaultWriteParam方法的典型用法代码示例。如果您正苦于以下问题:Java ImageWriter.getDefaultWriteParam方法的具体用法?Java ImageWriter.getDefaultWriteParam怎么用?Java ImageWriter.getDefaultWriteParam使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.imageio.ImageWriter的用法示例。


在下文中一共展示了ImageWriter.getDefaultWriteParam方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: compress

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static byte[] compress(BufferedImage image, float scale) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer = writers.next();
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(scale);
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(image, null, null), param);
    byte[] data = baos.toByteArray();
    writer.dispose();
    return data;
}
 
开发者ID:flschiavoni,项目名称:shareMySheet,代码行数:15,代码来源:ImageTransformation.java

示例2: saveImage

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void saveImage(final String fileName, final BufferedImage image, ImageFormat imageFormat) {
  try {
    final File file = new File(fileName);
    final String extension = FileUtilities.getExtension(fileName);
    Iterator<ImageWriter> iter = null;
    if (canWriteFormat(extension)) {
      iter = ImageIO.getImageWritersByFormatName(extension);
    } else {
      iter = ImageIO.getImageWritersByFormatName(imageFormat.toString());
    }

    final ImageWriter writer = iter.next();
    final ImageWriteParam iwp = writer.getDefaultWriteParam();

    file.getParentFile().mkdirs();
    try (final FileImageOutputStream output = new FileImageOutputStream(file.getAbsoluteFile())) {
      writer.setOutput(output);
      final IIOImage outimage = new IIOImage(image, null, null);
      writer.write(null, outimage, iwp);
      writer.dispose();
    }
  } catch (final IOException e) {
    log.log(Level.SEVERE, e.getMessage(), e);
  }
}
 
开发者ID:gurkenlabs,项目名称:litiengine,代码行数:26,代码来源:ImageSerializer.java

示例3: compress

import javax.imageio.ImageWriter; //导入方法依赖的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);
	}
}
 
开发者ID:wooui,项目名称:springboot-training,代码行数:37,代码来源:ImageUtil.java

示例4: compressSave

import javax.imageio.ImageWriter; //导入方法依赖的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);
}
 
开发者ID:Exercon,项目名称:AntiSocial-Platform,代码行数:18,代码来源:ImageServiceImpl.java

示例5: main

import javax.imageio.ImageWriter; //导入方法依赖的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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:WriteProgressive.java

示例6: createTestImage

import javax.imageio.ImageWriter; //导入方法依赖的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");
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:21,代码来源:LUTCompareTest.java

示例7: ImageCompressionTypesTest

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public ImageCompressionTypesTest(String format) {
    Iterator it = ImageIO.getImageWritersByFormatName(format);
    while (it.hasNext()) {
        writer = (ImageWriter) it.next();
        break;
    }
    ImageWriteParam param = writer.getDefaultWriteParam();

    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    System.out.println("Checking compression types for : " + format);
    String compTypes[] = param.getCompressionTypes();
    if (compTypes.length > 1) {
        for (int i = 0; i < compTypes.length; i++) {
            for (int j = i + 1; j < compTypes.length; j++) {
                if (compTypes[i].equalsIgnoreCase(compTypes[j])) {
                    throw new RuntimeException("Duplicate compression"
                            + " type exists for image format " + format);
                }
            }
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:ImageCompressionTypesTest.java

示例8: testEncode

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
@Test
public void testEncode() throws IOException {
  ImageWriter writer = Streams.stream(ImageIO.getImageWritersByFormatName("jpeg"))
      .filter(TurboJpegImageWriter.class::isInstance)
      .findFirst().get();
  ImageWriteParam param = writer.getDefaultWriteParam();
  param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  param.setCompressionQuality(0.85f);
  BufferedImage in = ImageIO.read(ClassLoader.getSystemResource("crop_aligned.jpg"));
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  try (ImageOutputStream ios = ImageIO.createImageOutputStream(os)) {
    writer.setOutput(ios);
    writer.write(null, new IIOImage(in, null, null), param);
  }
  os.flush();
  assertThat(os.toByteArray()).isNotEmpty();
}
 
开发者ID:dbmdz,项目名称:imageio-jnr,代码行数:18,代码来源:TurboJpegImageWriterTest.java

示例9: doTest

import javax.imageio.ImageWriter; //导入方法依赖的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.");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:CompressionModeTest.java

示例10: apply

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
@Override
public BufferedImage apply(BufferedImage img) {

	if(compressionLevel == 0f)
		return img;
	
	try {
		ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();
		JPEGImageWriteParam iwp = (JPEGImageWriteParam) iw.getDefaultWriteParam();
		iwp.setOptimizeHuffmanTables(false);
		iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
		iwp.setCompressionQuality(1f-compressionLevel);


		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		iw.setOutput(new MemoryCacheImageOutputStream(baos)); 

		IIOImage outputImage = new IIOImage(img, null, null);
		iw.write(null, outputImage, iwp);
		iw.dispose();
		
		baos.flush(); 
		byte[] returnImage = baos.toByteArray(); 
		baos.close();
		
		
		BufferedImage img2 = ImageIO.read(new ByteArrayInputStream(returnImage));
		
		if(img2 == null)
			throw new Exception();
		else
			img = img2;

	} catch (Exception e) {
		e.printStackTrace();
	}

	return img;
}
 
开发者ID:CalebKussmaul,项目名称:GIFKR,代码行数:41,代码来源:JPEGFilter.java

示例11: resize

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
/**
    * Reads the original image, creates a resized copy of it and returns its input stream. largestDimension is the
    * largest dimension of the resized image, the other dimension is scaled accordingly.
    *
    * @param image
    *            original image
    * @param largestDimension
    *            the largest dimension of the resized image, the other dimension is scaled accordingly
    * @return
    * @throws IOException
    * @throws UploadImageGalleryFileException
    */
   public static InputStream resize(BufferedImage image, int largestDimension) throws IOException {
try {
    //resize to 150 pixels max
    BufferedImage outImage = Scalr.resize(image, Method.QUALITY, largestDimension);
    
    //set jpeg compression quality explicitly
    ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam jpgWriteParam = imageWriter.getDefaultWriteParam();
    jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    jpgWriteParam.setCompressionQuality(.95f);

    // buffer all data in a circular buffer of infinite sizes
    CircularByteBuffer outputBuffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputBuffer.getOutputStream());
    imageWriter.setOutput(ios);
    IIOImage outputImage = new IIOImage(outImage, null, null);
    imageWriter.write(null, outputImage, jpgWriteParam);
    imageWriter.dispose();
    outputBuffer.getOutputStream().close();

    return outputBuffer.getInputStream();

} catch (IOException e) {
    log.error(e.getStackTrace());
    return null;
}
   }
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:40,代码来源:ResizePictureUtil.java

示例12: doTest

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public void doTest(int type) throws IOException {
    BufferedImage src = getTestImage(type);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    writer.setOutput(ios);

    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    switch(type) {
        case TEST_RLE8:
            wparam.setCompressionType("BI_RLE8");
            break;
        case TEST_RLE4:
            wparam.setCompressionType("BI_RLE4");
            break;
        default:
            throw new IllegalArgumentException("Wrong test type: " + type);
    }

    writer.write(null, new IIOImage(src, null, null), wparam);

    ios.close();
    baos.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

    BufferedImage dst = ImageIO.read(bais);

    checkResult(src, dst);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:RLECompressionTest.java

示例13: writeWithCompression

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void writeWithCompression(BufferedImage src,
                                         String compression) throws IOException
{
    System.out.println("Compression: " + compression);
    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    if (writer == null) {
        throw new RuntimeException("Test failed: no bmp writer available");
    }
    File fout = File.createTempFile(compression + "_", ".bmp",
                                    new File("."));

    ImageOutputStream ios = ImageIO.createImageOutputStream(fout);
    writer.setOutput(ios);

    BMPImageWriteParam param = (BMPImageWriteParam)
            writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compression);
    param.setTopDown(true);
    writer.write(null, new IIOImage(src, null, null), param);
    writer.dispose();
    ios.flush();
    ios.close();

    BufferedImage dst = ImageIO.read(fout);

    verify(dst);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:TopDownTest.java

示例14: main

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void main(String[] args) {
    try {
        AnimationTest t = new AnimationTest();
        t.initFrame();

        ImageWriter w = t.initWriter();

        ImageWriteParam p = w.getDefaultWriteParam();

        IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p);

        w.prepareWriteSequence(streamMetadata);

        for (int i = 0; i < 50; i++) {
            BufferedImage f = t.createNextFrame();

            ImageTypeSpecifier type = new ImageTypeSpecifier(f);

            IIOMetadata m = w.getDefaultImageMetadata(type, p);

            w.writeToSequence(new IIOImage(f, null, m), p);
        }
        w.endWriteSequence();

        t.checkAnimation();
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:AnimationTest.java

示例15: writeTestImage

import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void writeTestImage(String fileName) {
    BufferedImage bi =
        new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(new Color(testRGB));
    g.fillRect(0, 0, TEST_W, TEST_H);
    try {
        System.err.printf("Writing %s\n", fileName);
        if (lowCompression) {
            ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix("jpeg").next();
            if(iw == null) {
                throw new RuntimeException("No available image writer for "
                                           + "jpeg "
                                           + " Test failed.");
            }

            File file = new File(fileName);
            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
            iw.setOutput(ios);

            ImageWriteParam param = iw.getDefaultWriteParam();
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(1);

            IIOImage iioImg = new IIOImage(bi, null, null);
            iw.write(null, iioImg, param);

        } else {
            ImageIO.write(bi, "jpeg", new File(fileName));
        }
    } catch (IOException e) {
        System.err.println("Error " + e +
                           " when writing file: " + fileName);
        throw new RuntimeException(e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:37,代码来源:JPEGsNotAcceleratedTest.java


注:本文中的javax.imageio.ImageWriter.getDefaultWriteParam方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。