本文整理汇总了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;
}
示例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();
}
}
示例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();
}
示例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();
}
示例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);
}
示例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);
}
}
示例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;
}
}
示例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);
}
}