小鑫lay 2019-01-23 09:22 采纳率: 0%
浏览 1013
已结题

java 给图片打上水印发现,图片的大小缩小了原来是70KB打完水印变为30KB

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



    }
  • 写回答

1条回答 默认 最新

  • Rick漓殇 2019-01-23 10:27
    关注

    文件大小变了, 图片是否会模糊,是否是品质为问题?

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)