本文整理汇总了Java中com.sun.image.codec.jpeg.JPEGImageEncoder.setJPEGEncodeParam方法的典型用法代码示例。如果您正苦于以下问题:Java JPEGImageEncoder.setJPEGEncodeParam方法的具体用法?Java JPEGImageEncoder.setJPEGEncodeParam怎么用?Java JPEGImageEncoder.setJPEGEncodeParam使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.image.codec.jpeg.JPEGImageEncoder
的用法示例。
在下文中一共展示了JPEGImageEncoder.setJPEGEncodeParam方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: snapImageFile
import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入方法依赖的package包/类
void snapImageFile(String filename, int width, int height) {
BufferedImage bImage = doRender(width, height);
/*
* JAI: RenderedImage fImage = JAI.create("format", bImage,
* DataBuffer.TYPE_BYTE); JAI.create("filestore", fImage, filename +
* ".tif", "tiff", null);
*/
/* No JAI: */
try {
FileOutputStream fos = new FileOutputStream(filename + ".jpg");
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(bos);
JPEGEncodeParam param = jie.getDefaultJPEGEncodeParam(bImage);
param.setQuality(1.0f, true);
jie.setJPEGEncodeParam(param);
jie.encode(bImage);
bos.flush();
fos.close();
} catch (Exception e) {
System.out.println(e);
}
}
示例2: jdkResize
import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入方法依赖的package包/类
/**
* jdk压缩图片-质量压缩
*
* @param destImagePath 被压缩文件路径
* @param quality 压缩质量比例
* @return
* @throws Exception
*/
public static void jdkResize(String destImagePath, float quality) throws Exception {
// 目标文件
File resizedFile = new File(destImagePath);
// 压缩
Image targetImage = ImageIO.read(resizedFile);
int width = targetImage.getWidth(null);
int height = targetImage.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(targetImage, 0, 0, width, height, null);
g.dispose();
String ext = getFileType(resizedFile.getName());
if (ext.equals("jpg") || ext.equals("jpeg")) { // 如果是jpg
// jpeg格式的对输出质量进行设置
FileOutputStream out = new FileOutputStream(resizedFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(image);
jep.setQuality(quality, false);
encoder.setJPEGEncodeParam(jep);
encoder.encode(image);
out.close();
} else {
ImageIO.write(image, ext, resizedFile);
}
}
示例3: writeJPEGFile
import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入方法依赖的package包/类
/** Writes the current view in a <code>Canvas3D</code> object to an JPG file */
public static void writeJPEGFile(String fName, Canvas3D canvas){
GraphicsContext3D ctx = canvas.getGraphicsContext3D();
// The raster components need all be set!
Raster ras = new Raster(
new Point3f(-1.0f,-1.0f,-1.0f),
Raster.RASTER_COLOR,
0,0,
canvas.getWidth(),canvas.getHeight(),
new ImageComponent2D( ImageComponent.FORMAT_RGB, new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_RGB)),
null);
ctx.readRaster(ras);
// Now strip out the image info
BufferedImage img = ras.getImage().getImage();
// write that to disk....
try {
FileOutputStream out = new FileOutputStream(fName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
param.setQuality(0.95f,false); // 75% quality for the JPEG
encoder.setJPEGEncodeParam(param);
encoder.encode(img);
out.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
示例4: writeJPG
import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入方法依赖的package包/类
/**
* 输出为JPG文件
*
* @param o 图像
* @param output 输出文件
* @throws IOException
*/
public static void writeJPG(BufferedImage o,File output) throws IOException{
FileOutputStream out = new FileOutputStream(output);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(o);
param.setQuality(1f,false);
encoder.setJPEGEncodeParam(param);
encoder.encode(o);
out.close();
}
示例5: createThumbnail
import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入方法依赖的package包/类
private void createThumbnail(String pathImage, String pathThumbnail){
final int PREVIEW_WIDTH = 125;
final int PREVIEW_HEIGHT = 125;
File srcImage = new File(pathImage);
ImageIcon imageIcon = null;
if (srcImage != null) {
// Test if need to be loaded with JAI libraries (different
// format than JPG, PNG and GIF)
if (TFileUtils.isJAIRequired(srcImage)) {
// Load it with JAI class
RenderedOp src_aux = JAI.create("fileload", srcImage.getAbsolutePath());
BufferedImage bufferedImage = src_aux.getAsBufferedImage();
imageIcon = new ImageIcon(bufferedImage);
} else {
// Create it as usual
imageIcon = new ImageIcon(srcImage.getAbsolutePath());
}
}
File thum = new File (pathThumbnail);
if (!thum.exists()){
try{
int thumbWidth = PREVIEW_WIDTH;
int thumbHeight = PREVIEW_HEIGHT;
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = imageIcon.getIconWidth();
int imageHeight = imageIcon.getIconHeight();
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(imageIcon.getImage(), 0, 0, thumbWidth, thumbHeight, null);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(pathThumbnail));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
int quality = 100;
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
} catch (Exception ex) {
System.out.println("Error creating THUMBNAIL");
System.out.println(ex.toString());
}
}
}