當前位置: 首頁>>代碼示例>>Java>>正文


Java ImageWriteParam.setCompressionQuality方法代碼示例

本文整理匯總了Java中javax.imageio.ImageWriteParam.setCompressionQuality方法的典型用法代碼示例。如果您正苦於以下問題:Java ImageWriteParam.setCompressionQuality方法的具體用法?Java ImageWriteParam.setCompressionQuality怎麽用?Java ImageWriteParam.setCompressionQuality使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.imageio.ImageWriteParam的用法示例。


在下文中一共展示了ImageWriteParam.setCompressionQuality方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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

示例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: 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

示例7: 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

示例8: 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


注:本文中的javax.imageio.ImageWriteParam.setCompressionQuality方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。