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


Java JPEGImageWriteParam.setCompressionQuality方法代碼示例

本文整理匯總了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();
		}
	}
 
開發者ID:twak,項目名稱:chordatlas,代碼行數:18,代碼來源:Imagez.java

示例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);
}
 
開發者ID:nevesnunes,項目名稱:mid,代碼行數:18,代碼來源:Requester.java

示例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;
}
 
開發者ID:git-moss,項目名稱:Push2Display,代碼行數:19,代碼來源:ImageIOJPEGImageWriter.java

示例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);
    }
}
 
開發者ID:syvaidya,項目名稱:openstego,代碼行數:18,代碼來源:ImageUtil.java

示例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;
}
 
開發者ID:CalebKussmaul,項目名稱:GIFKR,代碼行數:41,代碼來源:JPEGFilter.java

示例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);
}
 
開發者ID:cycronix,項目名稱:cloudturbine,代碼行數:9,代碼來源:ImageTask.java

示例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);
}
 
開發者ID:esig,項目名稱:dss,代碼行數:15,代碼來源:ImageUtils.java

示例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;
}
 
開發者ID:meteoinfo,項目名稱:MeteoInfoLib,代碼行數:37,代碼來源:ChartPanel.java

示例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());
}
 
開發者ID:DaniUPC,項目名稱:near-image-replica-detection,代碼行數:41,代碼來源:ImageTools.java

示例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();
}
 
開發者ID:anonl,項目名稱:nvlist,代碼行數:30,代碼來源:DesktopJpegEncoder.java

示例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;
}
 
開發者ID:nyatla,項目名稱:NyARToolkit,代碼行數:46,代碼來源:JpegIO.java

示例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);
    }
}
 
開發者ID:sgoeschl,項目名稱:java-image-processing-survival-guide,代碼行數:41,代碼來源:AbstractJaiTest.java

示例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);
    }
}
 
開發者ID:sgoeschl,項目名稱:java-image-processing-survival-guide,代碼行數:40,代碼來源:BaseImageIoTest.java

示例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;
}
 
開發者ID:torakiki,項目名稱:sejda,代碼行數:8,代碼來源:JpegImageWriter.java

示例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();
    }
}
 
開發者ID:DiegoCatalano,項目名稱:Catalano-Framework,代碼行數:20,代碼來源:FastBitmap.java


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