`
flyPig
  • 浏览: 136996 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java处理图片

    博客分类:
  • Java
阅读更多
1.压缩图片
java下有直接方式可制作缩小图,最简单的方式就是等比例缩小宽、高,然后重新绘制保存。详细代码如下
/**
	 * @param srcURL 原图地址
	 * @param destURL 缩略图地址
	 * @param extractBase 压缩基数
	 * @param scale 压缩限制(宽/高)比例
	 * @throws Exception
	 */
	public void extractPhoto(String srcURL, String destURL, double extractBase,
			double scale) throws Exception {
		
		File srcFile = new File(srcURL);
		Image src = ImageIO.read(srcFile);
		int srcHeight = src.getHeight(null);
		int srcWidth = src.getWidth(null);
		int deskHeight = 0;
		int deskWidth = 0;
		double srcScale = (double) srcHeight / srcWidth;
		if ((double) srcHeight > extractBase || (double) srcWidth > extractBase) {
			if (srcScale >= scale || 1 / srcScale > scale) {
				if (srcScale >= scale) {
					deskHeight = (int) extractBase;
					deskWidth = srcWidth * deskHeight / srcHeight;
				} else {
					deskWidth = (int) extractBase;
					deskHeight = srcHeight * deskWidth / srcWidth;
				}
			} else {
				if ((double) srcHeight > extractBase) {
					deskHeight = (int) extractBase;
					deskWidth = srcWidth * deskHeight / srcHeight;
				} else {
					deskWidth = (int) extractBase;
					deskHeight = srcHeight * deskWidth / srcWidth;
				}
			}
		} else {
			deskHeight = srcHeight;
			deskWidth = srcWidth;

		}
		BufferedImage tag = new BufferedImage(deskWidth, deskHeight,
				BufferedImage.TYPE_3BYTE_BGR);
		tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null);
		FileOutputStream deskImage = new FileOutputStream(destURL); // 输出到文件流
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
		encoder.encode(tag);
		deskImage.close();
	}


2.给图片添加水印
/**
  * 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param markContent 水印的文字
* @param markContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
*/
public boolean createMark(String filePath,String markContent,Color markContentColor,float qualNum) 
{ 
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(markContentColor); 
g.setBackground(Color.white); 
g.drawImage(theImg, 0, 0, null ); 
g.drawString(markContent,width/5,height/5); //添加文字 
g.dispose(); 
try{ 
FileOutputStream out=new FileOutputStream(filePath); 
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); 
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); 
param.setQuality(qualNum, true); 
encoder.encode(bimage, param); 
out.close(); 
}catch(Exception e)
{ return false; } 
return true; 
} 
}


3.图片转换
jpg图片使用的是24-bit的编码,png有png-24和png-8两种,gif是8-bit的编码。如果强行将jpg图片信息流按字节拆开,转换成gif图片,即使使用标准256色,也会出现严重的失真.
转jpg代码比较简单。
public static void saveImageAsJpg (InputStream in, File saveFile,int width,int hight)    
            throws Exception {
        BufferedImage srcImage;

        srcImage = ImageIO.read(in);

        if(width > 0 || hight > 0)
        {
             srcImage = resize(srcImage, width, hight);
        }

        ImageIO.write(srcImage, "JPEG", saveFile);
        in.close();
    }

   public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
    // targetW,targetH分别表示目标长和宽
        int type = source.getType();
        BufferedImage target = null;
        double sx = (double) targetW / source.getWidth();
        double sy = (double) targetH / source.getHeight();

        //这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
        //则将下面的if else语句注释即可
        if(sx>sy)
        {
            sx = sy;
            targetW = (int)(sx * source.getWidth());
        }else{
            sy = sx;
            targetH = (int)(sy * source.getHeight());
        }

        if (type == BufferedImage.TYPE_CUSTOM) { //handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else
            target = new BufferedImage(targetW, targetH, type);
        Graphics2D g = target.createGraphics();
        //smoother than exlax:
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);


        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics