java处理图片

本文介绍如何使用Java进行图片处理,包括图片压缩、添加水印及格式转换等实用操作。通过等比例压缩和重新绘制实现图片压缩;利用自定义文字和颜色为图片添加水印;并提供将图片转换为JPG格式的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值