本文整理汇总了Java中com.sun.image.codec.jpeg.JPEGEncodeParam类的典型用法代码示例。如果您正苦于以下问题:Java JPEGEncodeParam类的具体用法?Java JPEGEncodeParam怎么用?Java JPEGEncodeParam使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JPEGEncodeParam类属于com.sun.image.codec.jpeg包,在下文中一共展示了JPEGEncodeParam类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: snapImageFile
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的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: writeAsJPEG
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
/**
* Write the jpeg image of the Component cnt to the OutPutStream.
*
* If the component is a penal with subpanels you might need to call
* {@link JPanel#addNotify} and {@link JPanel#doLayout}.
* If the server is running on Unix you will need to use java 1.4 and specify
* -Djava.awt.headless=true on the command Line or for tomcat:
* edit catalina.sh in the $CATALINA_HOME/bin directory and add a line
* CATALINA_OPTS='-Djava.awt.headless=true'
*/
static public void writeAsJPEG(OutputStream out, Component cnt)
throws IOException {
Dimension s = cnt.getSize();
BufferedImage bufferedImage = new BufferedImage((int) s.getWidth(),
(int) s.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.setColor(Color.white);
//graphics.fillRect(0, 0, (int)s.getWidth(), (int)s.getHeight());
cnt.print(graphics);
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(bufferedImage);
encodeParam.setQuality((float) 0.9, true);
jpegEncoder.encode(bufferedImage, encodeParam);
graphics.dispose();
bufferedImage.flush();
}
示例3: saveToPicture
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
/**
* ��һ��JPanel����Ϊ��fileָ����ͼƬ��
* @param file
* @param panel
*/
@SuppressWarnings("restriction")
public static void saveToPicture(File file, JComponent panel){
BufferedImage image = new BufferedImage(Page.PAGE_WIDTH, Page.PAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
panel.paint(g);
image.flush();
try {
FileOutputStream fos = new FileOutputStream(file);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(1.0f, true);
encoder.encode(image, param);
javax.imageio.ImageIO.write(image, "jpeg", fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例4: writeImage
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
/**
* @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream, org.apache.flex.forks.batik.ext.awt.image.spi.ImageWriterParams)
*/
public void writeImage(RenderedImage image, OutputStream out,
ImageWriterParams params) throws IOException {
BufferedImage bi;
if (image instanceof BufferedImage) {
bi = (BufferedImage)image;
} else {
//TODO Is this the right way?
bi = GraphicsUtil.makeLinearBufferedImage(
image.getWidth(), image.getHeight(), false);
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
if (params != null) {
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
if (params.getJPEGQuality() != null) {
param.setQuality(
params.getJPEGQuality().floatValue(),
params.getJPEGForceBaseline().booleanValue());
}
encoder.encode(bi, param);
} else {
encoder.encode(bi);
}
}
示例5: encode
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
/**
* Encodes an image in JPEG format and writes it to an OutputStream.
*
* @param bufferedImage The image to be encoded.
* @param outputStream The OutputStream to write the encoded image to.
* @throws IOException
*/
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.");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(this.quality, true);
encoder.encode(bufferedImage, param);
}
示例6: createMark
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
public static boolean createMark(String filePath, String printPath,
String markContent) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.red);
g.drawImage(theImg, 0, 0, null);
Font font = new Font(markContent, Font.BOLD, 200);
g.setFont(font);
g.setComposite(AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 0.5f));// 10%��
g.rotate(0.3f); // ���ֵ���ת�Ƕ�
g.drawString(markContent, width / 3, height / 3);// ����ˮӡ��λ��
g.dispose();
try {
FileOutputStream out = new FileOutputStream(printPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(100f, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
示例7: modifyEncodeParam
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
private static void modifyEncodeParam(JPEGEncodeParam src,
JPEGEncodeParam dst,
int nbands) {
dst.setDensityUnit (src.getDensityUnit());
dst.setXDensity (src.getXDensity());
dst.setYDensity (src.getYDensity());
dst.setRestartInterval(src.getRestartInterval());
for (int i=0; i<4; i++) {
JPEGQTable tbl = src.getQTable(i);
if (tbl != null)
dst.setQTable(i, tbl);
}
}
示例8: jdkResize
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的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);
}
}
示例9: writeJPEGFile
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的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();
}
}
示例10: writeJPG
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的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();
}
示例11: getJPEGEncodeParam
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的package包/类
/**
* Retrieves the JPEG compression parameters.
*/
public JPEGEncodeParam getJPEGEncodeParam() {
return jpegEncodeParam;
}
示例12: createThumbnail
import com.sun.image.codec.jpeg.JPEGEncodeParam; //导入依赖的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());
}
}
}