java 给图片打上水印发现,图片的大小缩小了原来70KB变为30KB 请问怎么实现给图片打完水印后图片的大小大于50KB 以下是打水印的代码
public static void markImageByTextNew(String channelNum,String timeChannel,String imgPath) {
InputStream is = null;
OutputStream os = null;
try {
// 源图片
Image srcImg = ImageIO.read(new File(imgPath));
double width = srcImg.getWidth(null);
//System.out.println("原图片的宽度"+width);
double height = srcImg.getHeight(null);
double xDivideY=width/height;
// System.out.println("原图片的高度"+height);
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
// 设置水印旋转
// 设置水印文字颜色
g.setColor(Color.gray);
//画图形检验原点位于图片左上角
/*g.setBackground(new Color(120,120,120));
g.clearRect(0, 0, width-100, height-200);*/
// 设置水印文字Font
//Math.pow(width, 2)宽的2次方
//Math.sqrt开平方
double length = Math.sqrt(Math.pow(width, 2)+Math.pow(height, 2));
//旋转角度
Integer degree = new Integer((int)(Math.atan((double)height/(double)width)/3.14*180+0.5));
length -= 30;
int size = (int)(length/25);
if (null != degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
g.setFont(new Font("宋体", Font.BOLD, size));
// 设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
0.9f));
String textWaterMark="仅供办理业务";
/*String sysTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String timeChannel=sysTime;*/
//String channelNum="12345678910";
//计算中文水印长度
int textLength=20*getLength(textWaterMark);
//计算右上角文字水印的y轴坐标
int textRightY=(int)Math.sqrt(Math.pow(textLength, 2)/(Math.pow(xDivideY, 2)+1));
int textrightX=(int)(xDivideY*textRightY);
//计算右上角文字水印的x轴坐标
int textRealX=(int)(width-textrightX);
int x = 0;
if(x == 0){
x = textRealX /2;
}else if(x > textRealX){
x = textRealX;
}
// System.out.println("usedX的长度为"+x);
//计算文字旋转后x、y方向的偏移量
int offsetY=(int)(textLength/2*(Math.sin(degree)))+size;
int offsetX=(int)(textLength/2*(Math.cos(degree)));
int pX1=textLength/2-offsetX-size;
int textRealY=offsetY+size*2;
// System.out.println("偏移的y的值为"+offsetY);
// 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(textWaterMark, x,textRealY);
//时间水印
int timeLength=5*getLength(timeChannel);
int rightY2=(int)Math.sqrt(Math.pow(timeLength, 2)/(Math.pow(xDivideY, 2)+1));
int usedY=(int)(height-rightY2);
//计算x、y偏移量
int timeOffsetY=(int)(timeLength/2*(Math.sin(degree)))+size;
int tRealOffsetX=(int)(timeLength/2*(Math.cos(degree)));
int timeRealOffsetX=tRealOffsetX+size*3;
int timeY=usedY-size;
g.drawString(timeChannel, timeRealOffsetX,timeY);
//渠道或操作员编号水印
int channelLength2=5*getLength(channelNum);
int rightY3=(int)Math.sqrt(Math.pow(channelLength2, 2)/(Math.pow(xDivideY, 2)+1));
int usedY2=(int)(height-rightY3);
int channelOffsetY=(int)(channelLength2/2*(Math.sin(degree)))+size;
int chanOffsetX=(int)(channelLength2/2*(Math.cos(degree)));
int channelOffsetX=chanOffsetX+size*5;
int channelY=usedY2;
g.drawString(channelNum, channelOffsetX,channelY);
// 释放资源
g.dispose();
// 生成图片
os = new FileOutputStream(imgPath);
ImageIO.write(buffImg, "JPG", os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}