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


Java ImageWriteParam.setCompressionMode方法代码示例

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


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

示例1: compress

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

示例3: 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();
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:33,代码来源:SunJPEGEncoderAdapter.java

示例4: 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);
    }
}
 
开发者ID:wooui,项目名称:springboot-training,代码行数:37,代码来源:ImageUtil.java

示例5: 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);
}
 
开发者ID:Exercon,项目名称:AntiSocial-Platform,代码行数:18,代码来源:ImageServiceImpl.java

示例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.");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:CompressionModeTest.java

示例7: testEncode

import javax.imageio.ImageWriteParam; //导入方法依赖的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

示例8: resize

import javax.imageio.ImageWriteParam; //导入方法依赖的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

示例9: getImageWriteParam

import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:BMPSubsamplingTest.java

示例10: main

import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
public static void main(String[] args) {
    ImageWriteParam iwp = new JPEGImageWriteParam(null);
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    float[] vals = iwp.getCompressionQualityValues();
    String[] descs = iwp.getCompressionQualityDescriptions();
    if (vals.length != (descs.length + 1)) {
        throw new RuntimeException("Test failed: Values array is not " +
                                   "one larger than descriptions array");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:CompressionVals.java

示例11: writeTestImage

import javax.imageio.ImageWriteParam; //导入方法依赖的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

示例12: doTest

import javax.imageio.ImageWriteParam; //导入方法依赖的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: doTest

import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
public void doTest() {
    try {
        System.out.println("Progress test for " + compression_type);
        BufferedImage bi = new BufferedImage(20, 300, BufferedImage.TYPE_INT_RGB);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

        Iterator iter = ImageIO.getImageWritersByFormatName(format);
        if (!iter.hasNext()) {
            throw new RuntimeException("No available writer for " + format);
        }
        ImageWriter writer = (ImageWriter)iter.next();

        writer.setOutput(ios);
        writer.addIIOWriteProgressListener(listener);

        IIOImage iio_img = new IIOImage(bi, null, null);

        ImageWriteParam param = writer.getDefaultWriteParam();

        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression_type);


        writer.write(null, iio_img, param);

        if (!listener.isTestPassed) {
            throw new RuntimeException("Test for " + compression_type + " does not finish correctly!");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:WriteProgressListenerTest.java

示例14: prepareWriteParam

import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
protected ImageWriteParam prepareWriteParam(BufferedImage src) {
    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    wparam.setCompressionType("BI_BITFIELDS");

    return wparam;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:TestCompressionBI_BITFIELDS.java

示例15: BMPSubsamplingTest

import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
public BMPSubsamplingTest() throws IOException {
    ImageWriter writer =
        ImageIO.getImageWritersByFormatName(format).next();

    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    String[] types = wparam.getCompressionTypes();
    for (int t = 0; t < img_types.length; t++) {
        int img_type = img_types[t];
        System.out.println("Test for " + getImageTypeName(img_type));
        BufferedImage image = getTestImage(img_type);

        ImageTypeSpecifier specifier = new ImageTypeSpecifier(image);

        if (!writer.getOriginatingProvider().canEncodeImage(specifier)) {
            System.out.println("Writer does not support encoding this buffered image type.");
            continue;
        }

        for(int i=0; i<types.length; i++) {
            if ("BI_JPEG".equals(types[i])) {
                // exclude BI_JPEG from automatic test
                // due to color diffusion effect on the borders.
                continue;
            }

            if (canEncodeImage(types[i], specifier, img_type)) {
                System.out.println("compression type: " + types[i] +
                    " Supported for " + getImageTypeName(img_type));
            } else {
                System.out.println("compression type: " + types[i] +
                    " NOT Supported for " + getImageTypeName(img_type));
                continue;
            }
            ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]);

            imageWriteParam.setSourceSubsampling(srcXSubsampling,
                                                 srcYSubsampling,
                                                 0, 0);
            File outputFile = new File("subsampling_test_" +
                getImageTypeName(img_type) + "__" +
                types[i] + ".bmp");
            ImageOutputStream ios =
                ImageIO.createImageOutputStream(outputFile);
            writer.setOutput(ios);

            IIOImage iioImg = new IIOImage(image, null, null);

            writer.write(null, iioImg, imageWriteParam);

            ios.flush();
            ios.close();

            BufferedImage outputImage = ImageIO.read(outputFile);
            checkTestImage(outputImage);
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:59,代码来源:BMPSubsamplingTest.java


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