本文整理匯總了Java中com.sun.image.codec.jpeg.JPEGCodec類的典型用法代碼示例。如果您正苦於以下問題:Java JPEGCodec類的具體用法?Java JPEGCodec怎麽用?Java JPEGCodec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JPEGCodec類屬於com.sun.image.codec.jpeg包,在下文中一共展示了JPEGCodec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: snapImageFile
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的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: compressImage
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
private void compressImage(int widthdist,int heightdist, Float rate) {
try {
if (rate != null && rate > 0) {
// 獲取文件高度和寬度
int[] results = getImageSize(this.srcFile);
if (results == null || results[0] == 0 || results[1] == 0) {
return;
} else {
widthdist = (int) (results[0] * rate);
heightdist = (int) (results[1] * rate);
}
}
// 開始讀取文件並進行壓縮
Image src = javax.imageio.ImageIO.read(this.srcFile);
BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(this.imgDist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
示例3: pressImage
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
/**
* 把圖片印刷到圖片上
*
* @param pressImg
* -- 水印文件
* @param targetImg
* -- 目標文件
* @param x
* @param y
*/
public final static void pressImage(String pressImg, String targetImg, int x, int y) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao - y, wideth_biao, height_biao, null);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例4: createSmallPhoto
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
public static void createSmallPhoto(String photoPath, String smallPath) {
File _file = new File(photoPath); // �����ļ�
Image src;
try {
src = javax.imageio.ImageIO.read(_file);
int wideth = 110; // �õ�Դͼ��
int height = 80; // �õ�Դͼ��
BufferedImage tag = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, wideth, height, null); // ������С���ͼ
FileOutputStream out = new FileOutputStream(smallPath); // ������ļ���
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // ��JPEG����
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例5: writeAsJPEG
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的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();
}
示例6: saveToPicture
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的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();
}
}
示例7: waterMark
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
/**
* 圖片水印
*
* @param imgPath
* 待處理圖片
* @param markPath
* 水印圖片
* @param x
* 水印位於圖片左上角的 x 坐標值
* @param y
* 水印位於圖片左上角的 y 坐標值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* */
public static void waterMark(String imgPath, String markPath, int x, int y, float alpha) {
try {
// 加載待處理圖片文件
Image img = ImageIO.read(new File(imgPath));
BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(img, 0, 0, null);
// 加載水印圖片文件
Image src_biao = ImageIO.read(new File(markPath));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(src_biao, x, y, null);
g.dispose();
// 保存處理後的文件
FileOutputStream out = new FileOutputStream(imgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: textMark
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
/**
* 文字水印
*
* @param imgPath 待處理圖片
* @param text 水印文字
* @param font 水印字體信息
* @param color 水印字體顏色
* @param x 水印位於圖片左上角的 x 坐標值
* @param y 水印位於圖片左上角的 y 坐標值
* @param alpha 水印透明度 0.1f ~ 1.0f
*/
public static void textMark(String imgPath, String text, Font font, Color color, int x, int y, float alpha) {
try {
Font Dfont = (font == null) ? new Font("宋體", 20, 13) : font;
Image img = ImageIO.read(new File(imgPath));
BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(img, 0, 0, null);
g.setColor(color);
g.setFont(Dfont);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawString(text, x, y);
g.dispose();
FileOutputStream out = new FileOutputStream(imgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
示例9: writeImage
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的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);
}
}
示例10: write
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
private void write(JComponent myComponent, OutputStream out)
throws Exception {
int imgWidth = (int) myComponent.getSize().getWidth(), imgHeight = (int) myComponent
.getSize().getHeight();
Dimension size = new Dimension(imgWidth, imgHeight);
BufferedImage myImage = new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
throw new Exception("GRAPHICS ERROR,CANNOT CREATE JPEG FORMAT");
}
}
示例11: encode
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的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);
}
示例12: main
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
public static void main() {
// in jdk.unsupported
Class<?> caller = Reflection.getCallerClass(2);
// removed
JPEGCodec r = new JPEGCodec();
// removed
SoftCache s = new SoftCache();
// removed
Service.providers(S.class);
}
示例13: zoomPic
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
public static String zoomPic(String outputDir, String outputFileName, int width, int height, boolean proportion,InputStream in) {
try {
Image img = ImageIO.read(in); //如果是本地圖片處理的話,這個地方直接把file放到ImageIO.read(file)中即可,如果是執行上傳圖片的話, Formfile formfile=獲得表單提交的Formfile ,然後 ImageIO.read 方法裏參數放 formfile.getInputStream()
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
return "no";
} else {
int newWidth; int newHeight;
// 判斷是否是等比縮放
if (proportion == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null)) / (double) width + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) height + 0.1;
// 根據縮放比率大的進行縮放控製
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = width; // 輸出的圖片寬度
newHeight = height; // 輸出的圖片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
// JPEGImageEncoder可適用於其他圖片類型的轉換
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
示例14: putWatermarkImage
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
/**
* 向一個目標圖片上覆蓋一個水印圖片
*
* @param markImgPath
* 水印圖片路徑
* @param targetImgPath
* 目標圖片路徑
* @param x
* 覆蓋位置橫坐標
* @param y
* 覆蓋位置縱坐標
* @throws IOException
* 讀寫異常
*/
public void putWatermarkImage(String markImgPath, String targetImgPath, int x, int y) throws IOException {
// 目標文件
File targetFile = new File(targetImgPath);
Image targetImage = ImageIO.read(targetFile);
int widthT = targetImage.getWidth(null);
int heightT = targetImage.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(widthT, heightT, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.createGraphics();
graphics.drawImage(targetImage, 0, 0, widthT, heightT, null);
// 水印文件
File markFile = new File(markImgPath);
Image markImage = ImageIO.read(markFile);
int widthM = markImage.getWidth(null);
int heightM = markImage.getHeight(null);
graphics.drawImage(markImage, x, y, widthM, heightM, null);
// 水印覆蓋
graphics.dispose();
FileOutputStream outputStream = new FileOutputStream(targetImgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(bufferedImage);
outputStream.close();
}
示例15: putWatermarkText
import com.sun.image.codec.jpeg.JPEGCodec; //導入依賴的package包/類
/**
* 向圖片中添加一個文件水印
*
* @param targetImgPath
* 待添加水印的圖片路徑
* @param markLabel
* 水印文本
* @param color
* 文本顏色
* @param font
* 文本字體
* @param x
* 水印位置橫坐標
* @param y
* 水印位置縱坐標
* @throws IOException
* 讀寫異常
*/
public void putWatermarkText(String targetImgPath, String markLabel, Color color, Font font, int x, int y) throws IOException {
File targetFile = new File(targetImgPath);
Image targetImage = ImageIO.read(targetFile);
int widthT = targetImage.getWidth(null);
int heightT = targetImage.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(widthT, heightT, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.createGraphics();
graphics.drawImage(targetImage, 0, 0, widthT, heightT, null);
graphics.setColor(color);
graphics.setFont(font);
graphics.drawString(markLabel, x, y);
graphics.dispose();
FileOutputStream outputStream = new FileOutputStream(targetImgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(bufferedImage);
outputStream.close();
/*
* 調用過程格式如下:
* utils.putWatermarkText("F:/IMG/GB/3.jpg", "HELLO WATERMARK!", Color.GREEN, new Font("Courier New", Font.BOLD, 20), 330, 230);
*/
}