本文整理汇总了Java中javax.imageio.plugins.jpeg.JPEGImageWriteParam.setCompressionQuality方法的典型用法代码示例。如果您正苦于以下问题:Java JPEGImageWriteParam.setCompressionQuality方法的具体用法?Java JPEGImageWriteParam.setCompressionQuality怎么用?Java JPEGImageWriteParam.setCompressionQuality使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.plugins.jpeg.JPEGImageWriteParam
的用法示例。
在下文中一共展示了JPEGImageWriteParam.setCompressionQuality方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeJPG
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的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();
}
}
示例2: downloadImage
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
public static void downloadImage(String imgURL, InputStream postInStr)
throws IOException {
BufferedImage image = ImageIO.read(postInStr);
String[] URLparts = imgURL.split("/");
File outputFile = new File(saveFolderName + "/" + URLparts[URLparts.length - 1]);
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(1f);
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
writer.setOutput(new FileImageOutputStream(outputFile));
writer.write(null, new IIOImage(image, null, null), jpegParams);
System.out.println("Got " + outputFile + " from " + imgURL);
}
示例3: getDefaultWriteParam
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
protected ImageWriteParam getDefaultWriteParam(
ImageWriter iiowriter, RenderedImage image,
ImageWriterParams params) {
JPEGImageWriteParam param = new JPEGImageWriteParam(iiowriter.getLocale());
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(params.getJPEGQuality());
if (params.getCompressionMethod() != null
&& !"JPEG".equals(params.getCompressionMethod())) {
throw new IllegalArgumentException(
"No compression method other than JPEG is supported for JPEG output!");
}
if (params.getJPEGForceBaseline()) {
param.setProgressiveMode(JPEGImageWriteParam.MODE_DISABLED);
}
return param;
}
示例4: writeJpegImage
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
private static void writeJpegImage(ImageHolder image, OutputStream os) throws OpenStegoException {
try {
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
Float qual = UserPreferences.getFloat("image.writer.jpeg.quality");
if (qual == null) {
qual = 0.75f;
}
jpegParams.setCompressionQuality(qual);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
writer.setOutput(ImageIO.createImageOutputStream(os));
writer.write(null, new IIOImage(image.getImage(), null, image.getMetadata()), jpegParams);
} catch (IOException e) {
throw new OpenStegoException(e);
}
}
示例5: apply
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的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;
}
示例6: ImageTask
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
public ImageTask(CTstream ctsI, DataStream dataStreamI) {
cts = ctsI;
dataStream = dataStreamI;
// Setup image quality
jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(cts.imageQuality);
}
示例7: convertToInputStreamJPG
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
private static ImageAndResolution convertToInputStreamJPG(BufferedImage buffImage, int dpi) throws IOException {
ImageWriter writer = getImageWriter("jpeg");
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) writer.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(1);
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, jpegParams);
initDpiJPG(metadata, dpi);
return getImageAndResolution(buffImage, dpi, writer, jpegParams, metadata);
}
示例8: saveImage_Jpeg
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
public boolean saveImage_Jpeg(String file, int width, int height, int dpi) {
//BufferedImage bufferedImage = this.mapBitmap;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
paintGraphics(g, width, height);
try {
// Image writer
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File(file));
imageWriter.setOutput(ios);
// Compression
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(0.85f);
// Metadata (dpi)
IIOMetadata data = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(bufferedImage), jpegParams);
Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
jfif.setAttribute("resUnits", "1"); // density is dots per inch
data.setFromTree("javax_imageio_jpeg_image_1.0", tree);
// Write and clean up
imageWriter.write(null, new IIOImage(bufferedImage, null, data), jpegParams);
ios.close();
imageWriter.dispose();
} catch (Exception e) {
return false;
}
return true;
}
示例9: getJPEGCompressedImage
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
public static Mat getJPEGCompressedImage(Mat source, float quality)
throws InvalidArgumentException, IOException {
if (quality < 0.0f || quality > 1.0f) {
throw new InvalidArgumentException("Quality is out of bounds: " + quality + ". Must be between 0.0 and 1.0");
}
// Define temporary pathfile path
String tempDir = System.getProperty("java.io.tmpdir");
String fileName = tempDir + File.separatorChar + ReplicaUtils.getRandomID() + "-q" + quality + ".jpeg";
// Save file
ReplicaUtils.matrixToFile(source, fileName);
// Read input image
InputStream iis = new FileInputStream(fileName);
RenderedImage image = ImageIO.read(iis);
// Define compression param
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(quality);
// Get default writer
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) writers.next();
// Specifies where the jpg image has to be written - byte array buffer
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
// Writes the file with given compression level
writer.write(null, new IIOImage(image, null, null), jpegParams);
// Delete temporary file
ReplicaUtils.deleteFile(fileName);
return ReplicaUtils.bytesToMatrix((int) source.size().height, (int)source.size().width, os.toByteArray());
}
示例10: encode
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
@Override
public byte[] encode(Pixmap pixmap, JpegEncoderParams params) throws IOException {
// Use an explicit image type to flatten any alpha in the source pixmap
BufferedImage image = BufferedImageHelper.toBufferedImage(pixmap, BufferedImage.TYPE_3BYTE_BGR);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageWriter writer = Iterators.get(ImageIO.getImageWritersByFormatName("jpg"), 0);
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
JPEGImageWriteParam iwparam = new JPEGImageWriteParam(Locale.ROOT);
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
iwparam.setCompressionQuality(params.getQuality());
iwparam.setOptimizeHuffmanTables(true);
// Write the image
writer.write(null, new IIOImage(image, null, null), iwparam);
// Cleanup
ios.flush();
} finally {
writer.dispose();
}
image.flush();
return out.toByteArray();
}
示例11: encode
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
/**
* http://stackoverflow.com/questions/233504/write-dpi-metadata-to-a-jpeg-image-in-java
* @param w
* @param h
* @param i_x_dpi
* @param i_y_dpi
* @param i_dpi_unit
* @param i_src
* @param i_quority
* @return
* @throws IOException
*/
public static byte[] encode(int w,int h,int i_x_dpi,int i_y_dpi,int i_dpi_unit,byte[] i_src,float i_quority) throws IOException
{
BufferedImage img = new BufferedImage(w ,h ,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster wr=img.getRaster();
DataBufferByte buf=(DataBufferByte)wr.getDataBuffer();
System.arraycopy(i_src, 0, buf.getData(),0,w*h);
ByteArrayOutputStream bout=new ByteArrayOutputStream();
JPEGImageWriter jw=(JPEGImageWriter)ImageIO.getImageWritersBySuffix("jpeg").next();
jw.setOutput(new MemoryCacheImageOutputStream(bout));
// Compression
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) jw.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(i_quority);
// Metadata (dpi)
IIOMetadata data = jw.getDefaultImageMetadata(new ImageTypeSpecifier(img), jpegParams);
Element tree = (Element)data.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(i_x_dpi));
jfif.setAttribute("Ydensity", Integer.toString(i_y_dpi));
jfif.setAttribute("resUnits", Integer.toString(i_dpi_unit)); // density is dots per inch
data.setFromTree("javax_imageio_jpeg_image_1.0",tree);
// Write and clean up
jw.write(null, new IIOImage(img, null, data), jpegParams);
byte[] ret=bout.toByteArray();
bout.close();
jw.dispose();
return ret;
}
示例12: writeBufferedImage
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
@Override
public void writeBufferedImage(BufferedImage bufferedImage, float quality, int dpi, String formatName, File targetFile) throws Exception {
if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO.getImageWritersBySuffix(formatName).next();
ImageWriteParam writeParam = imageWriter.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = imageWriter.getDefaultImageMetadata(typeSpecifier, writeParam);
if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
Element tree = (Element) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
jfif.setAttribute("resUnits", "1");
metadata.setFromTree("javax_imageio_jpeg_image_1.0", tree);
}
if (quality >= 0 && quality <= 1f) {
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(quality);
}
FileOutputStream os = new FileOutputStream(targetFile);
final ImageOutputStream stream = ImageIO.createImageOutputStream(os);
try {
imageWriter.setOutput(stream);
imageWriter.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
} finally {
stream.close();
}
} else {
writeBufferedImage(bufferedImage, formatName, targetFile);
}
}
示例13: writeBufferedImage
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
@Override
public void writeBufferedImage(BufferedImage bufferedImage, float quality, int dpi, String formatName, File targetFile) throws Exception {
System.out.println("Saving " + targetFile.getPath());
if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO.getImageWritersBySuffix(formatName).next();
ImageWriteParam writeParam = imageWriter.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = imageWriter.getDefaultImageMetadata(typeSpecifier, writeParam);
if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
Element tree = (Element) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
jfif.setAttribute("resUnits", "1");
metadata.setFromTree("javax_imageio_jpeg_image_1.0", tree);
}
if (quality >= 0 && quality <= 1f) {
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(quality);
}
FileOutputStream os = new FileOutputStream(targetFile);
final ImageOutputStream stream = ImageIO.createImageOutputStream(os);
try {
imageWriter.setOutput(stream);
imageWriter.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
} finally {
stream.close();
}
} else {
writeBufferedImage(bufferedImage, formatName, targetFile);
}
}
示例14: newImageWriterParams
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
@Override
ImageWriteParam newImageWriterParams(PdfToJpegParameters params) {
JPEGImageWriteParam param = new JPEGImageWriteParam(writer.getLocale());
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality((float) params.getQuality() / 100);
return param;
}
示例15: saveAsJPG
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; //导入方法依赖的package包/类
/**
* Save FastBitmap as JPG.
* @param pathname Path name.
* @param quality Quality.
*/
public void saveAsJPG(String pathname, float quality){
try {
JPEGImageWriteParam params = new JPEGImageWriteParam(null);
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionQuality(quality);
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
writer.setOutput(new FileImageOutputStream(new File(pathname)));
writer.write(null, new IIOImage(bufferedImage, null, null), params);
} catch (IOException ex) {
//Logger.getLogger(FastBitmap.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}