入职第九天 框架中的UUID生成及图片处理截取字符串

早上主要学习了框架中的工具类:
包括为了生成UUID所需要的将bytes[]转成Int,以及java图像的获取,放大,缩小及图像水印。

byte[]转Int

//bytes 转换为  int int 32位范围[-2^32,2^32)  byte 8位 最用范围是[-2^8,2^8)
    public static int toInt(byte[] bytes) {
        int result = 0;
        //遍历四次
        for (int i = 0; i < 4; i++) {
            //<<左位移运算(在二进制状态下向左移动8位) 2^8+x
            //思想:1Int是4个byte,要将byte转换为int,就算byte为0 也至少满足10000000100000001000000010000000这样的形式
            //然后每一位byte上具体是多少只要在10000000上添加即可
            //如果字节数组超过4个转化为Int时由于数据过大,导致无法完全转换为Int,所以最终显示Int范围内的数据
            result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
        }
        return result;
    }

图像处理

/** *//**
     * 缩放图像
     * @param srcImageFile 源图像文件地址
     * @param result       缩放后的图像地址
     * @param scale        缩放比例
     * @param flag         缩放选择:true 放大; false 缩小;
     */
    public static void scale(String srcImageFile, String result, int scale, boolean flag)
    {
        try
       {
           BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
            int width = src.getWidth(); // 得到源图宽
            int height = src.getHeight(); // 得到源图长
            if (flag)
            {
                // 放大
                width = width * scale;
                height = height * scale;
            }
            else
            {
                // 缩小
                width = width / scale;
                height = height / scale;
            }
            Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            ImageIO.write(tag, "JPG", new File(result));// 输出到文件流
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

图片水印文字

/**
      * 添加文字水印
      * @param targetImg 目标图片路径,如:C://myPictrue//1.jpg
      * @param pressText 水印文字, 如:中国证券网
      * @param fontName 字体名称,    如:宋体
      * @param fontStyle 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)
      * @param fontSize 字体大小,单位为像素
      * @param color 字体颜色
      * @param x 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间
      * @param y 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间
      * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
      */
      public static void pressText( File file, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha) {
         try {
             Image image = ImageIO.read(file);
             int width = image.getWidth(null);
             int height = image.getHeight(null);
             BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
             Graphics2D g = bufferedImage.createGraphics();
             g.drawImage(image, 0, 0, width, height, null);
             g.setFont(new Font(fontName, fontStyle, fontSize));
             g.setColor(color);
             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
             
             int width_1 = fontSize * getLength(pressText);
             int height_1 = fontSize;
             int widthDiff = width - width_1;
             int heightDiff = height - height_1;
             if(x < 0){
                 x = widthDiff / 2;
             }else if(x > widthDiff){
                 x = widthDiff;
             }
             if(y < 0){
                 y = heightDiff / 2;
             }else if(y > heightDiff){
                 y = heightDiff;
             }
             
             g.drawString(pressText, x, y + height_1);
             g.dispose();
             ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
         
     /**
      * 获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符
      * @param text
      * @return 字符长度,如:text="中国",返回 2;text="test",返回 2;text="中国ABC",返回 4.
      * @throws UnsupportedEncodingException 
      */
     public static int getLength(String text) throws UnsupportedEncodingException {
         int textLength = text.length();
         int length = textLength;
         for (int i = 0; i < textLength; i++) {
             if (String.valueOf(text.charAt(i)).getBytes("utf-8").length > 1) {
                 length++;
             }
         }
         return (length % 2 == 0) ? length / 2 : length / 2 + 1;
     }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,773评论 19 139
  • 第六届社区文化节终于如期开幕了。虽然这个如期可能会让竞争对手贻笑大方,毕竟这个如期已经是推迟到了落叶纷飞的秋季。 ...
    ld熊壮壮阅读 244评论 0 0
  • 前两天下午,正好在帮江西一位客户淘车,微信一直响,结果一看是去年出差认识的一位湖南妹子: 记得当时走遍了长沙的5个...
    LV学阅读 1,404评论 0 1

友情链接更多精彩内容