zoukankan      html  css  js  c++  java
  • ImagesUtil java图片操作 缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等

    1. import java.awt.AlphaComposite;  
    2. import java.awt.Color;  
    3. import java.awt.Font;  
    4. import java.awt.Graphics;  
    5. import java.awt.Graphics2D;  
    6. import java.awt.Image;  
    7. import java.awt.Toolkit;  
    8. import java.awt.color.ColorSpace;  
    9. import java.awt.geom.AffineTransform;  
    10. import java.awt.image.AffineTransformOp;  
    11. import java.awt.image.BufferedImage;  
    12. import java.awt.image.ColorConvertOp;  
    13. import java.awt.image.CropImageFilter;  
    14. import java.awt.image.FilteredImageSource;  
    15. import java.awt.image.ImageFilter;  
    16. import java.io.File;  
    17. import java.io.IOException;  
    18.   
    19. import javax.imageio.ImageIO;  
    20.   
    21. /** 
    22.  * 图片处理工具类:<br> 
    23.  * 功能:缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等 
    24.  *  
    25.  * @author alexgaoyh 
    26.  */  
    27. public class ImagesUtil {  
    28.   
    29.     /** 
    30.      * 几种常见的图片格式 
    31.      */  
    32.     public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式  
    33.     public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组  
    34.     public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组  
    35.     public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式  
    36.     public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形  
    37.     public static String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop  
    38.   
    39.     /** 
    40.      * 程序入口:用于测试 
    41.      *  
    42.      * @param args 
    43.      */  
    44.     public static void main(String[] args) {  
    45.         // 1-缩放图像:  
    46.         // 方法一:按比例缩放  
    47.         ImagesUtil.scale("f:/2.gif""f:/_scale.jpg"11false);// 测试OK  
    48.         // 方法二:按高度和宽度缩放  
    49.         //ImagesUtil.scale2("f:/2.gif", "f:/abc_scale2.jpg", 236, 50, true);// 测试OK  
    50.   
    51.         // 2-切割图像:  
    52.         // 方法一:按指定起点坐标和宽高切割  
    53.         //ImagesUtil.cut("e:/abc.jpg", "e:/abc_cut.jpg", 0, 0, 400, 400);// 测试OK  
    54.         // 方法二:指定切片的行数和列数  
    55.         //ImagesUtil.cut2("e:/abc.jpg", "e:/", 2, 2);// 测试OK  
    56.         // 方法三:指定切片的宽度和高度  
    57.         //ImagesUtil.cut3("e:/abc.jpg", "e:/", 300, 300);// 测试OK  
    58.   
    59.         // 3-图像类型转换:  
    60.         //ImagesUtil.convert("e:/abc.jpg", "GIF", "e:/abc_convert.gif");// 测试OK  
    61.   
    62.         // 4-彩色转黑白:  
    63.         //ImagesUtil.gray("e:/abc.jpg", "e:/abc_gray.jpg");// 测试OK  
    64.   
    65.         // 5-给图片添加文字水印:  
    66.         // 方法一:  
    67.         //ImagesUtil.pressText("我是水印文字", "e:/abc.jpg", "e:/abc_pressText.jpg", "宋体", Font.BOLD, Color.white, 80, 0, 0, 0.5f);// 测试OK  
    68.         // 方法二:  
    69.         //ImagesUtil.pressText2("我也是水印文字", "e:/abc.jpg", "e:/abc_pressText2.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.5f);// 测试OK  
    70.   
    71.         // 6-给图片添加图片水印:  
    72.         ImagesUtil.pressImage("e:/abc2.jpg""e:/abc.jpg",  
    73.                 "e:/abc_pressImage.jpg"000.5f);// 测试OK  
    74.     }  
    75.   
    76.     /** 
    77.      * 缩放图像(按比例缩放) 
    78.      *  
    79.      * @param srcImageFile 
    80.      *            源图像文件地址 
    81.      * @param result 
    82.      *            缩放后的图像地址 
    83.      * @param scale 
    84.      *            缩放比例 
    85.      * @param flag 
    86.      *            缩放选择:true 放大; false 缩小; 
    87.      */  
    88.     public final static void scale(String srcImageFile, String result,  
    89.             int scale, boolean flag) {  
    90.         try {  
    91.             BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件  
    92.             int width = src.getWidth(); // 得到源图宽  
    93.             int height = src.getHeight(); // 得到源图长  
    94.             if (flag) {// 放大  
    95.                 width = width * scale;  
    96.                 height = height * scale;  
    97.             } else {// 缩小  
    98.                 width = width / scale;  
    99.                 height = height / scale;  
    100.             }  
    101.             Image image = src.getScaledInstance(width, height,  
    102.                     Image.SCALE_DEFAULT);  
    103.             BufferedImage tag = new BufferedImage(width, height,  
    104.                     BufferedImage.TYPE_INT_RGB);  
    105.             Graphics g = tag.getGraphics();  
    106.             g.drawImage(image, 00null); // 绘制缩小后的图  
    107.             g.dispose();  
    108.             ImageIO.write(tag, "JPEG"new File(result));// 输出到文件流  
    109.         } catch (IOException e) {  
    110.             e.printStackTrace();  
    111.         }  
    112.     }  
    113.   
    114.     /** 
    115.      * 缩放图像(按高度和宽度缩放) 
    116.      *  
    117.      * @param srcImageFile 
    118.      *            源图像文件地址 
    119.      * @param result 
    120.      *            缩放后的图像地址 
    121.      * @param height 
    122.      *            缩放后的高度 
    123.      * @param width 
    124.      *            缩放后的宽度 
    125.      * @param bb 
    126.      *            比例不对时是否需要补白:true为补白; false为不补白; 
    127.      */  
    128.     public final static void scale2(String srcImageFile, String result,  
    129.             int height, int width, boolean bb) {  
    130.         try {  
    131.             double ratio = 0.0// 缩放比例  
    132.             File f = new File(srcImageFile);  
    133.             BufferedImage bi = ImageIO.read(f);  
    134.             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);  
    135.             // 计算比例  
    136.             if ((bi.getHeight() > height) || (bi.getWidth() > width)) {  
    137.                 if (bi.getHeight() > bi.getWidth()) {  
    138.                     ratio = (new Integer(height)).doubleValue()  
    139.                             / bi.getHeight();  
    140.                 } else {  
    141.                     ratio = (new Integer(width)).doubleValue() / bi.getWidth();  
    142.                 }  
    143.                 AffineTransformOp op = new AffineTransformOp(AffineTransform  
    144.                         .getScaleInstance(ratio, ratio), null);  
    145.                 itemp = op.filter(bi, null);  
    146.             }  
    147.             if (bb) {// 补白  
    148.                 BufferedImage image = new BufferedImage(width, height,  
    149.                         BufferedImage.TYPE_INT_RGB);  
    150.                 Graphics2D g = image.createGraphics();  
    151.                 g.setColor(Color.white);  
    152.                 g.fillRect(00, width, height);  
    153.                 if (width == itemp.getWidth(null))  
    154.                     g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,  
    155.                             itemp.getWidth(null), itemp.getHeight(null),  
    156.                             Color.white, null);  
    157.                 else  
    158.                     g.drawImage(itemp, (width - itemp.getWidth(null)) / 20,  
    159.                             itemp.getWidth(null), itemp.getHeight(null),  
    160.                             Color.white, null);  
    161.                 g.dispose();  
    162.                 itemp = image;  
    163.             }  
    164.             ImageIO.write((BufferedImage) itemp, "JPEG"new File(result));  
    165.         } catch (IOException e) {  
    166.             e.printStackTrace();  
    167.         }  
    168.     }  
    169.   
    170.     /** 
    171.      * 图像切割(按指定起点坐标和宽高切割) 
    172.      *  
    173.      * @param srcImageFile 
    174.      *            源图像地址 
    175.      * @param result 
    176.      *            切片后的图像地址 
    177.      * @param x 
    178.      *            目标切片起点坐标X 
    179.      * @param y 
    180.      *            目标切片起点坐标Y 
    181.      * @param width 
    182.      *            目标切片宽度 
    183.      * @param height 
    184.      *            目标切片高度 
    185.      */  
    186.     public final static void cut(String srcImageFile, String result, int x,  
    187.             int y, int width, int height) {  
    188.         try {  
    189.             // 读取源图像  
    190.             BufferedImage bi = ImageIO.read(new File(srcImageFile));  
    191.             int srcWidth = bi.getHeight(); // 源图宽度  
    192.             int srcHeight = bi.getWidth(); // 源图高度  
    193.             if (srcWidth > 0 && srcHeight > 0) {  
    194.                 Image image = bi.getScaledInstance(srcWidth, srcHeight,  
    195.                         Image.SCALE_DEFAULT);  
    196.                 // 四个参数分别为图像起点坐标和宽高  
    197.                 // 即: CropImageFilter(int x,int y,int width,int height)  
    198.                 ImageFilter cropFilter = new CropImageFilter(x, y, width,  
    199.                         height);  
    200.                 Image img = Toolkit.getDefaultToolkit().createImage(  
    201.                         new FilteredImageSource(image.getSource(), cropFilter));  
    202.                 BufferedImage tag = new BufferedImage(width, height,  
    203.                         BufferedImage.TYPE_INT_RGB);  
    204.                 Graphics g = tag.getGraphics();  
    205.                 g.drawImage(img, 00, width, height, null); // 绘制切割后的图  
    206.                 g.dispose();  
    207.                 // 输出为文件  
    208.                 ImageIO.write(tag, "JPEG"new File(result));  
    209.             }  
    210.         } catch (Exception e) {  
    211.             e.printStackTrace();  
    212.         }  
    213.     }  
    214.   
    215.     /** 
    216.      * 图像切割(指定切片的行数和列数) 
    217.      *  
    218.      * @param srcImageFile 
    219.      *            源图像地址 
    220.      * @param descDir 
    221.      *            切片目标文件夹 
    222.      * @param rows 
    223.      *            目标切片行数。默认2,必须是范围 [1, 20] 之内 
    224.      * @param cols 
    225.      *            目标切片列数。默认2,必须是范围 [1, 20] 之内 
    226.      */  
    227.     public final static void cut2(String srcImageFile, String descDir,  
    228.             int rows, int cols) {  
    229.         try {  
    230.             if (rows <= 0 || rows > 20)  
    231.                 rows = 2// 切片行数  
    232.             if (cols <= 0 || cols > 20)  
    233.                 cols = 2// 切片列数  
    234.             // 读取源图像  
    235.             BufferedImage bi = ImageIO.read(new File(srcImageFile));  
    236.             int srcWidth = bi.getHeight(); // 源图宽度  
    237.             int srcHeight = bi.getWidth(); // 源图高度  
    238.             if (srcWidth > 0 && srcHeight > 0) {  
    239.                 Image img;  
    240.                 ImageFilter cropFilter;  
    241.                 Image image = bi.getScaledInstance(srcWidth, srcHeight,  
    242.                         Image.SCALE_DEFAULT);  
    243.                 int destWidth = srcWidth; // 每张切片的宽度  
    244.                 int destHeight = srcHeight; // 每张切片的高度  
    245.                 // 计算切片的宽度和高度  
    246.                 if (srcWidth % cols == 0) {  
    247.                     destWidth = srcWidth / cols;  
    248.                 } else {  
    249.                     destWidth = (int) Math.floor(srcWidth / cols) + 1;  
    250.                 }  
    251.                 if (srcHeight % rows == 0) {  
    252.                     destHeight = srcHeight / rows;  
    253.                 } else {  
    254.                     destHeight = (int) Math.floor(srcWidth / rows) + 1;  
    255.                 }  
    256.                 // 循环建立切片  
    257.                 // 改进的想法:是否可用多线程加快切割速度  
    258.                 for (int i = 0; i < rows; i++) {  
    259.                     for (int j = 0; j < cols; j++) {  
    260.                         // 四个参数分别为图像起点坐标和宽高  
    261.                         // 即: CropImageFilter(int x,int y,int width,int height)  
    262.                         cropFilter = new CropImageFilter(j * destWidth, i  
    263.                                 * destHeight, destWidth, destHeight);  
    264.                         img = Toolkit.getDefaultToolkit().createImage(  
    265.                                 new FilteredImageSource(image.getSource(),  
    266.                                         cropFilter));  
    267.                         BufferedImage tag = new BufferedImage(destWidth,  
    268.                                 destHeight, BufferedImage.TYPE_INT_RGB);  
    269.                         Graphics g = tag.getGraphics();  
    270.                         g.drawImage(img, 00null); // 绘制缩小后的图  
    271.                         g.dispose();  
    272.                         // 输出为文件  
    273.                         ImageIO.write(tag, "JPEG"new File(descDir + "_r" + i  
    274.                                 + "_c" + j + ".jpg"));  
    275.                     }  
    276.                 }  
    277.             }  
    278.         } catch (Exception e) {  
    279.             e.printStackTrace();  
    280.         }  
    281.     }  
    282.   
    283.     /** 
    284.      * 图像切割(指定切片的宽度和高度) 
    285.      *  
    286.      * @param srcImageFile 
    287.      *            源图像地址 
    288.      * @param descDir 
    289.      *            切片目标文件夹 
    290.      * @param destWidth 
    291.      *            目标切片宽度。默认200 
    292.      * @param destHeight 
    293.      *            目标切片高度。默认150 
    294.      */  
    295.     public final static void cut3(String srcImageFile, String descDir,  
    296.             int destWidth, int destHeight) {  
    297.         try {  
    298.             if (destWidth <= 0)  
    299.                 destWidth = 200// 切片宽度  
    300.             if (destHeight <= 0)  
    301.                 destHeight = 150// 切片高度  
    302.             // 读取源图像  
    303.             BufferedImage bi = ImageIO.read(new File(srcImageFile));  
    304.             int srcWidth = bi.getHeight(); // 源图宽度  
    305.             int srcHeight = bi.getWidth(); // 源图高度  
    306.             if (srcWidth > destWidth && srcHeight > destHeight) {  
    307.                 Image img;  
    308.                 ImageFilter cropFilter;  
    309.                 Image image = bi.getScaledInstance(srcWidth, srcHeight,  
    310.                         Image.SCALE_DEFAULT);  
    311.                 int cols = 0// 切片横向数量  
    312.                 int rows = 0// 切片纵向数量  
    313.                 // 计算切片的横向和纵向数量  
    314.                 if (srcWidth % destWidth == 0) {  
    315.                     cols = srcWidth / destWidth;  
    316.                 } else {  
    317.                     cols = (int) Math.floor(srcWidth / destWidth) + 1;  
    318.                 }  
    319.                 if (srcHeight % destHeight == 0) {  
    320.                     rows = srcHeight / destHeight;  
    321.                 } else {  
    322.                     rows = (int) Math.floor(srcHeight / destHeight) + 1;  
    323.                 }  
    324.                 // 循环建立切片  
    325.                 // 改进的想法:是否可用多线程加快切割速度  
    326.                 for (int i = 0; i < rows; i++) {  
    327.                     for (int j = 0; j < cols; j++) {  
    328.                         // 四个参数分别为图像起点坐标和宽高  
    329.                         // 即: CropImageFilter(int x,int y,int width,int height)  
    330.                         cropFilter = new CropImageFilter(j * destWidth, i  
    331.                                 * destHeight, destWidth, destHeight);  
    332.                         img = Toolkit.getDefaultToolkit().createImage(  
    333.                                 new FilteredImageSource(image.getSource(),  
    334.                                         cropFilter));  
    335.                         BufferedImage tag = new BufferedImage(destWidth,  
    336.                                 destHeight, BufferedImage.TYPE_INT_RGB);  
    337.                         Graphics g = tag.getGraphics();  
    338.                         g.drawImage(img, 00null); // 绘制缩小后的图  
    339.                         g.dispose();  
    340.                         // 输出为文件  
    341.                         ImageIO.write(tag, "JPEG"new File(descDir + "_r" + i  
    342.                                 + "_c" + j + ".jpg"));  
    343.                     }  
    344.                 }  
    345.             }  
    346.         } catch (Exception e) {  
    347.             e.printStackTrace();  
    348.         }  
    349.     }  
    350.   
    351.     /** 
    352.      * 图像类型转换:GIF->JPG、GIF->PNG、PNG->JPG、PNG->GIF(X)、BMP->PNG 
    353.      *  
    354.      * @param srcImageFile 
    355.      *            源图像地址 
    356.      * @param formatName 
    357.      *            包含格式非正式名称的 String:如JPG、JPEG、GIF等 
    358.      * @param destImageFile 
    359.      *            目标图像地址 
    360.      */  
    361.     public final static void convert(String srcImageFile, String formatName,  
    362.             String destImageFile) {  
    363.         try {  
    364.             File f = new File(srcImageFile);  
    365.             f.canRead();  
    366.             f.canWrite();  
    367.             BufferedImage src = ImageIO.read(f);  
    368.             ImageIO.write(src, formatName, new File(destImageFile));  
    369.         } catch (Exception e) {  
    370.             e.printStackTrace();  
    371.         }  
    372.     }  
    373.   
    374.     /** 
    375.      * 彩色转为黑白 
    376.      *  
    377.      * @param srcImageFile 
    378.      *            源图像地址 
    379.      * @param destImageFile 
    380.      *            目标图像地址 
    381.      */  
    382.     public final static void gray(String srcImageFile, String destImageFile) {  
    383.         try {  
    384.             BufferedImage src = ImageIO.read(new File(srcImageFile));  
    385.             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
    386.             ColorConvertOp op = new ColorConvertOp(cs, null);  
    387.             src = op.filter(src, null);  
    388.             ImageIO.write(src, "JPEG"new File(destImageFile));  
    389.         } catch (IOException e) {  
    390.             e.printStackTrace();  
    391.         }  
    392.     }  
    393.   
    394.     /** 
    395.      * 给图片添加文字水印 
    396.      *  
    397.      * @param pressText 
    398.      *            水印文字 
    399.      * @param srcImageFile 
    400.      *            源图像地址 
    401.      * @param destImageFile 
    402.      *            目标图像地址 
    403.      * @param fontName 
    404.      *            水印的字体名称 
    405.      * @param fontStyle 
    406.      *            水印的字体样式 
    407.      * @param color 
    408.      *            水印的字体颜色 
    409.      * @param fontSize 
    410.      *            水印的字体大小 
    411.      * @param x 
    412.      *            修正值 
    413.      * @param y 
    414.      *            修正值 
    415.      * @param alpha 
    416.      *            透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字 
    417.      */  
    418.     public final static void pressText(String pressText, String srcImageFile,  
    419.             String destImageFile, String fontName, int fontStyle, Color color,  
    420.             int fontSize, int x, int y, float alpha) {  
    421.         try {  
    422.             File img = new File(srcImageFile);  
    423.             Image src = ImageIO.read(img);  
    424.             int width = src.getWidth(null);  
    425.             int height = src.getHeight(null);  
    426.             BufferedImage image = new BufferedImage(width, height,  
    427.                     BufferedImage.TYPE_INT_RGB);  
    428.             Graphics2D g = image.createGraphics();  
    429.             g.drawImage(src, 00, width, height, null);  
    430.             g.setColor(color);  
    431.             g.setFont(new Font(fontName, fontStyle, fontSize));  
    432.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,  
    433.                     alpha));  
    434.             // 在指定坐标绘制水印文字  
    435.             g.drawString(pressText, (width - (getLength(pressText) * fontSize))  
    436.                     / 2 + x, (height - fontSize) / 2 + y);  
    437.             g.dispose();  
    438.             ImageIO.write((BufferedImage) image, "JPEG",  
    439.                     new File(destImageFile));// 输出到文件流  
    440.         } catch (Exception e) {  
    441.             e.printStackTrace();  
    442.         }  
    443.     }  
    444.   
    445.     /** 
    446.      * 给图片添加文字水印 
    447.      *  
    448.      * @param pressText 
    449.      *            水印文字 
    450.      * @param srcImageFile 
    451.      *            源图像地址 
    452.      * @param destImageFile 
    453.      *            目标图像地址 
    454.      * @param fontName 
    455.      *            字体名称 
    456.      * @param fontStyle 
    457.      *            字体样式 
    458.      * @param color 
    459.      *            字体颜色 
    460.      * @param fontSize 
    461.      *            字体大小 
    462.      * @param x 
    463.      *            修正值 
    464.      * @param y 
    465.      *            修正值 
    466.      * @param alpha 
    467.      *            透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字 
    468.      */  
    469.     public final static void pressText2(String pressText, String srcImageFile,  
    470.             String destImageFile, String fontName, int fontStyle, Color color,  
    471.             int fontSize, int x, int y, float alpha) {  
    472.         try {  
    473.             File img = new File(srcImageFile);  
    474.             Image src = ImageIO.read(img);  
    475.             int width = src.getWidth(null);  
    476.             int height = src.getHeight(null);  
    477.             BufferedImage image = new BufferedImage(width, height,  
    478.                     BufferedImage.TYPE_INT_RGB);  
    479.             Graphics2D g = image.createGraphics();  
    480.             g.drawImage(src, 00, width, height, null);  
    481.             g.setColor(color);  
    482.             g.setFont(new Font(fontName, fontStyle, fontSize));  
    483.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,  
    484.                     alpha));  
    485.             // 在指定坐标绘制水印文字  
    486.             g.drawString(pressText, (width - (getLength(pressText) * fontSize))  
    487.                     / 2 + x, (height - fontSize) / 2 + y);  
    488.             g.dispose();  
    489.             ImageIO.write((BufferedImage) image, "JPEG",  
    490.                     new File(destImageFile));  
    491.         } catch (Exception e) {  
    492.             e.printStackTrace();  
    493.         }  
    494.     }  
    495.   
    496.     /** 
    497.      * 给图片添加图片水印 
    498.      *  
    499.      * @param pressImg 
    500.      *            水印图片 
    501.      * @param srcImageFile 
    502.      *            源图像地址 
    503.      * @param destImageFile 
    504.      *            目标图像地址 
    505.      * @param x 
    506.      *            修正值。 默认在中间 
    507.      * @param y 
    508.      *            修正值。 默认在中间 
    509.      * @param alpha 
    510.      *            透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字 
    511.      */  
    512.     public final static void pressImage(String pressImg, String srcImageFile,  
    513.             String destImageFile, int x, int y, float alpha) {  
    514.         try {  
    515.             File img = new File(srcImageFile);  
    516.             Image src = ImageIO.read(img);  
    517.             int wideth = src.getWidth(null);  
    518.             int height = src.getHeight(null);  
    519.             BufferedImage image = new BufferedImage(wideth, height,  
    520.                     BufferedImage.TYPE_INT_RGB);  
    521.             Graphics2D g = image.createGraphics();  
    522.             g.drawImage(src, 00, wideth, height, null);  
    523.             // 水印文件  
    524.             Image src_biao = ImageIO.read(new File(pressImg));  
    525.             int wideth_biao = src_biao.getWidth(null);  
    526.             int height_biao = src_biao.getHeight(null);  
    527.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,  
    528.                     alpha));  
    529.             g.drawImage(src_biao, (wideth - wideth_biao) / 2,  
    530.                     (height - height_biao) / 2, wideth_biao, height_biao, null);  
    531.             // 水印文件结束  
    532.             g.dispose();  
    533.             ImageIO.write((BufferedImage) image, "JPEG",  
    534.                     new File(destImageFile));  
    535.         } catch (Exception e) {  
    536.             e.printStackTrace();  
    537.         }  
    538.     }  
    539.   
    540.     /** 
    541.      * 计算text的长度(一个中文算两个字符) 
    542.      *  
    543.      * @param text 
    544.      * @return 
    545.      */  
    546.     public final static int getLength(String text) {  
    547.         int length = 0;  
    548.         for (int i = 0; i < text.length(); i++) {  
    549.             if (new String(text.charAt(i) + "").getBytes().length > 1) {  
    550.                 length += 2;  
    551.             } else {  
    552.                 length += 1;  
    553.             }  
    554.         }  
    555.         return length / 2;  
    556.     }  
    557. }  
  • 相关阅读:
    VMWare中Red Hat Enterprise Linux 6与Windows XP共享文件夹/共享目录
    PetShop 4.0的数据库
    Eclipse安装Perl插件
    安装GCC for Red Hat Enterprise Linux Server release 6(64位)
    下载 Microsoft .NET Pet Shop 4.0
    一步一步安装 Microsoft .NET Pet Shop 4.0
    Visual Studio 2008安装ASP.NET MVC 2 RTM
    HTML标签大全(常用)
    换了个公司
    Counterfeit Dollar(poj1013暴力枚举)
  • 原文地址:https://www.cnblogs.com/huanjianlin/p/3012745.html
Copyright © 2011-2022 走看看