入職第九天 框架中的UUID生成及圖片處理截取字符串

早上主要學(xué)習(xí)了框架中的工具類:
包括為了生成UUID所需要的將bytes[]轉(zhuǎn)成Int,以及java圖像的獲取,放大,縮小及圖像水印。

byte[]轉(zhuǎn)Int

//bytes 轉(zhuǎn)換為  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++) {
            //<<左位移運算(在二進制狀態(tài)下向左移動8位) 2^8+x
            //思想:1Int是4個byte,要將byte轉(zhuǎn)換為int,就算byte為0 也至少滿足10000000100000001000000010000000這樣的形式
            //然后每一位byte上具體是多少只要在10000000上添加即可
            //如果字節(jié)數(shù)組超過4個轉(zhuǎn)化為Int時由于數(shù)據(jù)過大,導(dǎo)致無法完全轉(zhuǎn)換為Int,所以最終顯示Int范圍內(nèi)的數(shù)據(jù)
            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 目標(biāo)圖片路徑,如:C://myPictrue//1.jpg
      * @param pressText 水印文字, 如:中國證券網(wǎng)
      * @param fontName 字體名稱,    如:宋體
      * @param fontStyle 字體樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC)
      * @param fontSize 字體大小,單位為像素
      * @param color 字體顏色
      * @param x 水印文字距離目標(biāo)圖片左側(cè)的偏移量,如果x<0, 則在正中間
      * @param y 水印文字距離目標(biāo)圖片上側(cè)的偏移量,如果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;
     }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • 第六屆社區(qū)文化節(jié)終于如期開幕了。雖然這個如期可能會讓競爭對手貽笑大方,畢竟這個如期已經(jīng)是推遲到了落葉紛飛的秋季。 ...
    ld熊壯壯閱讀 241評論 0 0
  • 前兩天下午,正好在幫江西一位客戶淘車,微信一直響,結(jié)果一看是去年出差認(rèn)識的一位湖南妹子: 記得當(dāng)時走遍了長沙的5個...
    LV學(xué)閱讀 1,382評論 0 1

友情鏈接更多精彩內(nèi)容