当前位置: 首页>>代码示例>>Java>>正文


Java JPEGImageEncoder类代码示例

本文整理汇总了Java中com.sun.image.codec.jpeg.JPEGImageEncoder的典型用法代码示例。如果您正苦于以下问题:Java JPEGImageEncoder类的具体用法?Java JPEGImageEncoder怎么用?Java JPEGImageEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


JPEGImageEncoder类属于com.sun.image.codec.jpeg包,在下文中一共展示了JPEGImageEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
  }
}
 
开发者ID:HOMlab,项目名称:QN-ACTR-Release,代码行数:27,代码来源:LineTypes.java

示例2: compressImage

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
    }
}
 
开发者ID:Fetax,项目名称:Fetax-AI,代码行数:25,代码来源:CompressImageHelper.java

示例3: pressImage

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
    }
}
 
开发者ID:beihua,项目名称:DataBird,代码行数:36,代码来源:ImagePressTextUtil.java

示例4: createSmallPhoto

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
	}
}
 
开发者ID:java-scott,项目名称:java-project,代码行数:19,代码来源:ImageUtils.java

示例5: writeAsJPEG

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
}
 
开发者ID:mrmaxent,项目名称:Maxent,代码行数:29,代码来源:PlotServlet.java

示例6: saveToPicture

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
	} 
}
 
开发者ID:squallyou,项目名称:LScore,代码行数:24,代码来源:ImageHandler.java

示例7: waterMark

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
    }
}
 
开发者ID:ishare,项目名称:SimilarImageSearch,代码行数:39,代码来源:ImageHelper.java

示例8: textMark

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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);
    }
}
 
开发者ID:ishare,项目名称:SimilarImageSearch,代码行数:35,代码来源:ImageHelper.java

示例9: writeImage

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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);
    }
}
 
开发者ID:BowlerHatLLC,项目名称:feathers-sdk,代码行数:27,代码来源:JPEGImageWriter.java

示例10: write

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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");
	}
}
 
开发者ID:vonpower,项目名称:VonRep,代码行数:18,代码来源:PanelDialog.java

示例11: encode

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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);
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:16,代码来源:SunJPEGEncoderAdapter.java

示例12: zoomPic

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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";   
}
 
开发者ID:yulele166,项目名称:pub-service,代码行数:39,代码来源:ImageZoomUtils.java

示例13: putWatermarkImage

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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();
}
 
开发者ID:William-Hai,项目名称:JCore-UtilLibrary,代码行数:42,代码来源:ImageOverlayUtils.java

示例14: putWatermarkText

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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);
     */
}
 
开发者ID:William-Hai,项目名称:JCore-UtilLibrary,代码行数:45,代码来源:ImageOverlayUtils.java

示例15: createMark

import com.sun.image.codec.jpeg.JPEGImageEncoder; //导入依赖的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;
}
 
开发者ID:java-scott,项目名称:java-project,代码行数:32,代码来源:ImageUtils.java


注:本文中的com.sun.image.codec.jpeg.JPEGImageEncoder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。