1、多个图片合成
2、裁剪图片
3、图片压缩
4、制作圆角
5、重调图片尺寸
6、获取图片尺寸
7、图片缩放
8、导入网络图片到缓冲区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | /** * @Description TestImageUtil.java * @author 张军 * @date 2017年6月16日 下午6:10:01 * @version V1.0 */ package zj.image; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.RoundRectangle2D; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.Collection; import javax.imageio.ImageIO; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import zj.check.util.CheckUtil; import zj.common.exception.ServiceException; import zj.java.util.JavaUtil; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * 纯JAVA实现的图片处理工具类 * * @version 1.00 (2014.09.15) * @author SHNKCS 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>} */ public class ImageUtil { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(ImageUtil. class ); /** * 导入网络图片到缓冲区 * * @param imgUrl * 网络路径 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return 图片缓冲区 */ public static BufferedImage loadImageUrl(String imgUrl) { try { // 获取网络地址 URL url = new URL(imgUrl); // 返回网络图片缓冲区 return ImageIO.read(url); } catch (Exception e) { logger.error( "导入网络图片到缓冲区" , e); } return null ; } /** * 图片缩放 * * @see #resize(String, String, int, int) * @param src * 源文件路径 * @param dest * 目标文件路径 * @param width * 宽度 * @param height * 高度 */ @Deprecated public static void zoomImage(String src, String dest, int width, int height) { try { double wr = 0 , hr = 0 ; File srcFile = new File(src); File destFile = new File(dest); File targetDir = destFile.getParentFile(); if (targetDir != null && !targetDir.exists()) { // 创建目标文件目录 targetDir.mkdirs(); } BufferedImage bufImg = ImageIO.read(srcFile); Image Itemp = bufImg.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); wr = width * 1.0 / bufImg.getWidth(); hr = height * 1.0 / bufImg.getHeight(); AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null ); Itemp = ato.filter(bufImg, null ); ImageIO.write((BufferedImage) Itemp, dest.substring(dest.lastIndexOf( "." ) + 1 ), destFile); } catch (Exception e) { throw new ServiceException(e); } } /** * 压缩图片 * * @see #resize(String, String, int, int) * @param src * 源文件路径 * @param dest * 目标文件路径 * @param width * 宽度 * @param height * 高度 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ @Deprecated public static void imageScale(String src, String dest, int width, int height) { BufferedOutputStream output = null ; try { File srcFile = new File(src); File destFile = new File(dest); File targetDir = destFile.getParentFile(); if (targetDir != null && !targetDir.exists()) { // 创建目标文件目录 targetDir.mkdirs(); } Image image = javax.imageio.ImageIO.read(srcFile); image = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING); // Make a BufferedImage from the Image. BufferedImage mBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = mBufferedImage.createGraphics(); g2.drawImage(image, 0 , 0 , width, height, Color.WHITE, null ); g2.dispose(); float [] kernelData2 = { - 0 .125f, - 0 .125f, - 0 .125f, - 0 .125f, 2 , - 0 .125f, - 0 .125f, - 0 .125f, - 0 .125f }; Kernel kernel = new Kernel( 3 , 3 , kernelData2); ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null ); mBufferedImage = cOp.filter(mBufferedImage, null ); output = new BufferedOutputStream( new FileOutputStream(destFile)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output); encoder.encode(mBufferedImage); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(output); } } /** * 获取图片尺寸(宽,高) * * @param filePath * 文件路径 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 * @return [width,height] */ public static int [] getSizeInfo(String filePath) { try { // 获取图片文件 File file = new File(filePath); // 获取缓冲区 Image image = ImageIO.read(file); // 返回宽度 int width = image.getWidth( null ); // 返回高度 int height = image.getHeight( null ); return new int [] { width, height }; } catch (Exception e) { throw new ServiceException(e); } } /** * 重调图片尺寸 * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param width * 新的宽度,小于1则忽略,按原图比例缩放 * @param height * 新的高度,小于1则忽略,按原图比例缩放 * @param maxWidth * 最大宽度,限制目标图片宽度,小于1则忽略此设置 * @param maxHeight * 最大高度,限制目标图片高度,小于1则忽略此设置 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void resize(File srcFile, File destFile, int width, int height, int maxWidth, int maxHeight) { InputStream input = null ; OutputStream output = null ; try { // 获取文件输入流 input = new BufferedInputStream( new FileInputStream(srcFile)); // 获取文件输出流 output = new BufferedOutputStream( new FileOutputStream(destFile)); if (width < 1 && height < 1 && maxWidth < 1 && maxHeight < 1 ) { // 如果全部小于1尺寸 IOUtils.copy(input, output); } // 获取源图片缓冲区 BufferedImage img = ImageIO.read(input); // 源图片宽度 double tempWidth = img.getWidth( null ); // 源图片高度 double tempHeight = img.getHeight( null ); // 目标图片宽度 int toWidth = - 1 ; // 目标图片高度 int toHeight = - 1 ; // 源图片宽高比例 double rate = tempWidth / tempHeight; if (width > 0 && height > 0 ) { // 如果输出的宽高大于0,比例设置为宽高比 rate = (( double ) width) / (( double ) height); // 设置目标图片宽高为输出的宽高 toWidth = width; toHeight = height; } else if (width > 0 ) { // 如果输出的宽大于0 // 设置目标图片宽为输出的宽 toWidth = width; // 设置目标图片高为输出的宽比上源图片宽高比 toHeight = ( int ) (toWidth / rate); } else if (height > 0 ) { // 如果输出的高大于0 // 设置目标图片高为输出的高 toHeight = height; // 设置目标图片宽为输出的高比上源图片宽高比 toWidth = ( int ) (toHeight * rate); } else { // 否则设置源图片宽 toWidth = ((Number) tempWidth).intValue(); // 否则设置源图片高 toHeight = ((Number) tempHeight).intValue(); } if (maxWidth > 0 && toWidth > maxWidth) { // 如果最大宽高大于 0并且输出的宽大于最大宽度 // 设置输出的宽为最大宽度 toWidth = maxWidth; // 设置输出的高为输出的宽比上源图片宽高比 toHeight = ( int ) (toWidth / rate); } if (maxHeight > 0 && toHeight > maxHeight) { // 如果最大高高大于 0并且输出的高大于最大高度 // 设置输出的高为最大高度 toHeight = maxHeight; // 设置输出的宽为输出的高比上源图片宽高比 toWidth = ( int ) (toHeight * rate); } // 判断透明色 boolean hasNotAlpha = img.getColorModel().hasAlpha(); // 获取透明色 int typeInt = hasNotAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; // 创建一个带透明色的BufferedImage对象:BufferedImage.TYPE_INT_ARGB // 创建一个不带透明色的BufferedImage对象:BufferedImage.TYPE_INT_RGB // 源图片缓冲区 BufferedImage tag = new BufferedImage(toWidth, toHeight, typeInt); // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 tag.getGraphics().drawImage(img.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH), 0 , 0 , null ); // 获取源文件扩展名 String srcExtName = FilenameUtils.getExtension(srcFile.getName()); // 输出源图片缓冲区到输出的文件中 ImageIO.write(tag, srcExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } /** * 重调图片尺寸(此方法图片更清楚) * * @param src * 源文件路径 * @param dest * 目标文件路径 * @param width * 新的宽度,小于1则忽略,按原图比例缩放 * @param height * 新的高度,小于1则忽略,按原图比例缩放 * @param maxWidth * 新的最大宽度,限制目标图片宽度,按原图比例缩放 * @param maxHeight * 新的最大高度,限制目标图片高度,按原图比例缩放 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void resize(String src, String dest, int width, int height, int maxWidth, int maxHeight) { resize( new File(src), new File(dest), width, height, maxWidth, maxHeight); } /** * 重调图片尺寸(此方法图片更清楚) * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param width * 新的宽度,小于1则忽略,按原图比例缩放 * @param height * 新的高度,小于1则忽略,按原图比例缩放 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void resize(File srcFile, File destFile, int width, int height) { resize(srcFile, destFile, width, height, - 1 , - 1 ); } /** * 重调图片尺寸(此方法图片更清楚) * * @param src * 源文件路径 * @param dest * 目标文件路径 * @param width * 新的宽度,小于1则忽略,按原图比例缩放 * @param height * 新的高度,小于1则忽略,按原图比例缩放 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void resize(String src, String dest, int width, int height) { resize( new File(src), new File(dest), width, height); } /** * 裁剪图片 * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param x * x坐标 * @param y * y坐标 * @param width * 裁剪宽度 * @param height * 裁剪高度 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void crop(File srcFile, File destFile, int x, int y, int width, int height) { OutputStream output = null ; InputStream input = null ; try { // 获取源图片输入流 input = new BufferedInputStream( new FileInputStream(srcFile)); // 获取目标图片输入流 output = new BufferedOutputStream( new FileOutputStream(destFile)); // 获取源图片缓冲区 BufferedImage srcImg = ImageIO.read(input); // 获取源图片宽度 int srcWidth = srcImg.getWidth(); // 获取源图片高度 int srcHeight = srcImg.getHeight(); // 计算临时x轴 int tempX = Math.min(srcWidth - 1 , x); // 计算临时y轴 int tempY = Math.min(srcHeight - 1 , y); // 输出的临时宽度 int tempWidth = width; if (tempX + width > srcWidth) { // 临时的x轴+输出的宽度大于源文件宽度 // 临时宽度设置源图片的宽度-临时x轴与1相比的最大值 tempWidth = Math.max( 1 , srcWidth - tempX); } // 输出的临时高度 int tempHeight = height; if (tempY + height > srcHeight) { // 临时的y轴+输出的高度大于源文件高度 // 临时高度设置源图片的高度-临时y轴与1相比的最大值 tempHeight = Math.max( 1 , srcHeight - tempY); } // 通过源图片的缓冲区获取目标文件缓冲区 BufferedImage dest = srcImg.getSubimage(tempX, tempY, tempWidth, tempHeight); // 判断透明色 boolean hasNotAlpha = srcImg.getColorModel().hasAlpha(); // 获取透明色 int typeInt = hasNotAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; // 创建一个带透明色的BufferedImage对象:BufferedImage.TYPE_INT_ARGB // 创建一个不带透明色的BufferedImage对象:BufferedImage.TYPE_INT_RGB // 源图片缓冲区 BufferedImage tag = new BufferedImage(width, height, typeInt); // 开始裁剪 tag.getGraphics().drawImage(dest, 0 , 0 , null ); // 获取源文件扩展名 String srcExtName = FilenameUtils.getExtension(srcFile.getName()); // 输出源图片缓冲区到输出的文件中 ImageIO.write(tag, srcExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } /** * 裁剪图片 * * @param src * 源文件路径 * @param dest * 目标文件路径 * @param x * x坐标 * @param y * y坐标 * @param width * 裁剪宽度 * @param height * 裁剪高度 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void crop(String src, String dest, int x, int y, int width, int height) throws Exception { crop( new File(src), new File(dest), x, y, width, height); } /** * 制作圆角 * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param cornerRadius * 角度 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void makeRoundedCorner(File srcFile, File destFile, int cornerRadius) { InputStream input = null ; OutputStream output = null ; try { // 获取源图片输入流 input = new BufferedInputStream( new FileInputStream(srcFile)); // 获取目标图片输入流 output = new BufferedOutputStream( new FileOutputStream(destFile)); // 获取源图片缓冲区 BufferedImage srcImg = ImageIO.read(input); // 获取源图片宽度 int srcWidth = srcImg.getWidth(); // 获取源图片高度 int srcHeight = srcImg.getHeight(); cornerRadius = cornerRadius < 1 ? srcWidth / 4 : cornerRadius; // 创建一个带透明色的BufferedImage对象:BufferedImage.TYPE_INT_ARGB // 创建一个不带透明色的BufferedImage对象:BufferedImage.TYPE_INT_RGB // 源图片缓冲区 BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_ARGB); // Graphics2D ,Graphics 类,提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。 // 它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。验证码生成可以用到此类。 // public abstract class Graphics2Dextends Graphics 此 Graphics2D 类扩展了 Graphics 类, // 提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。 Graphics2D g2 = tag.createGraphics(); // This is what we want, but it only does hard-clipping, i.e. // aliasing // g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip // shape // in fully opaque white with antialiasing enabled... // Java 2D允许分配透明(alpha)值,以便底层的图形可以显示出来。通常我们会创建一个java.awt.AlphaComposite对象,然后传入 setComposite()方法的实现。 g2.setComposite(AlphaComposite.Src); // 其他渲染hints适用在不同的环境下。如缩放图片时,为KEY_INTERPOLATION使用 VALUE_INTERPOLATION_BILINEAR。请查阅本类的Javadoc,详细了解各个选项所适用的环境。 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 设置颜色 g2.setColor(Color.WHITE); // 开始制作 // 前两个指定矩形的左上角的坐标 // 第3,4个指定矩形的宽度和高度 // 最后两个指定圆角弧形的宽度和高度 g2.fill( new RoundRectangle2D.Float( 0 , 0 , srcWidth, srcHeight, cornerRadius, cornerRadius)); // ... then compositing the image on top, // using the white shape from above as alpha source // Java 2D允许分配透明(alpha)值,以便底层的图形可以显示出来。通常我们会创建一个java.awt.AlphaComposite对象,然后传入 setComposite()方法的实现。 g2.setComposite(AlphaComposite.SrcAtop); // 开始画图片 g2.drawImage(srcImg, 0 , 0 , null ); // 释放资源 g2.dispose(); // 输出源图片缓冲区到输出的文件中(圆角只能是png) ImageIO.write(tag, "png" , output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } } /** * 制作圆角 * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param cornerRadius * 角度 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void makeRoundedCorner(String src, String dest, int cornerRadius) { makeRoundedCorner( new File(src), new File(dest), cornerRadius); } /** * 修改图片,返回修改后的图片缓冲区(只输出一行文本) * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param font * 字体 * @param fontColor * 字体颜色 * @param fontSize * 字体大小 * @param x * x坐标 * @param y * y坐标 * @param onlyLine * 是否一行输出文本,默认false:多行输出 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ @SuppressWarnings ( "unchecked" ) public static void writeImageString(File srcFile, File destFile, Font font, Color fontColor, int fontSize, Object content, int x, int y, boolean onlyLine) { InputStream input = null ; OutputStream output = null ; try { // 获取源图片输入流 input = new BufferedInputStream( new FileInputStream(srcFile)); // 获取文件输出流 output = new BufferedOutputStream( new FileOutputStream(destFile)); // 获取源图片缓冲区 BufferedImage srcImg = ImageIO.read(input); // 获取源图片宽度 int srcWidth = srcImg.getWidth(); // 获取源图片高度 int srcHeight = srcImg.getHeight(); // Graphics2D ,Graphics 类,提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。 // 它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。验证码生成可以用到此类。 // public abstract class Graphics2Dextends Graphics 此 Graphics2D 类扩展了 Graphics 类, // 提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。 Graphics2D g = srcImg.createGraphics(); // 设置背景为白色 g.setBackground(Color.WHITE); if (fontColor == null ) { // 设置字体颜色为蓝色 fontColor = Color.BLUE; } // 设置字体颜色为蓝色 g.setColor(fontColor); // ont是JAVA中的字体类,PLAIN是Font类中的静态常量( static final ) , // 表示是:普通样式常量。其他可用样式为:BOLD :粗体样式常量 ,ITALIC: 斜体样式常量.如可以如下初始化对象: // Font textFont = new Font("宋体" , Font.BOLD , 23);该字体表示23磅粗体的宋体字。 if (font == null ) { // 设置字体 font = new Font( "微软雅黑" , Font.PLAIN, fontSize); } // 设置字体 g.setFont(font); // 字体大小 // 计算临时x轴 int tempX = x; // 计算临时y轴 int tempY = y; // 验证输出位置的纵坐标和横坐标 if (x >= srcHeight || y >= srcWidth) { // 临时x轴为源图片高度-文字大小+2 tempX = srcHeight - fontSize + 2 ; // 临时y轴为源图片宽度 tempY = srcWidth; } if (content instanceof Collection) { Collection<String> contentColl = (Collection<String>) content; int contentLength = contentColl.size(); if (onlyLine) { boolean first = true ; for (String scontent : contentColl) { if (first) { if (x < 0 ) { // 写到右下角 tempX = srcWidth - scontent.length() * contentLength - fontSize - 50 ; } if (y < 0 ) { // 写到右下角 tempY = srcHeight - fontSize - 10 ; } first = false ; } g.drawString(scontent, tempX, tempY); tempX += scontent.length() * fontSize / 2 + 5 ; // 重新计算文本输出位置 } } else { boolean first = true ; for (String scontent : contentColl) { if (first) { if (x < 0 ) { // 写到右下角 tempX = srcWidth - scontent.length() - fontSize - 50 ; } if (y < 0 ) { // 写到右下角 tempY = srcHeight - fontSize * contentLength - 10 ; } first = false ; } g.drawString(scontent, tempX, tempY); tempY += fontSize + 2 ; // 重新计算文本输出位置 } } } else { String scontent = JavaUtil.objToStr(content); if (CheckUtil.isNotNull(scontent)) { if (x < 0 ) { // 写到右下角 tempX = srcWidth - scontent.length() - fontSize - 30 ; } if (y < 0 ) { // 写到右下角 tempY = srcHeight - fontSize - 10 ; } // 在图片上写文字 g.drawString(scontent, tempX, tempY); } } // 释放资源 g.dispose(); // 获取源文件扩展名 String srcExtName = FilenameUtils.getExtension(srcFile.getName()); // 输出源图片缓冲区到输出的文件中 ImageIO.write(srcImg, srcExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } /** * 修改图片,返回修改后的图片缓冲区(只输出一行文本) * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param fontColor * 字体颜色 * @param fontSize * 字体大小 * @param x * x坐标 * @param y * y坐标 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageString(File srcFile, File destFile, Color fontColor, int fontSize, Object content, int x, int y) { writeImageString(srcFile, destFile, null , fontColor, fontSize, content, x, y, false ); } /** * 修改图片,返回修改后的图片缓冲区(只输出一行文本) * * @param src * 源文件路径 * @param dest * 目标文件路径 * @param fontColor * 字体颜色 * @param fontSize * 字体大小 * @param x * x坐标 * @param y * y坐标 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageString(String src, String dest, Color fontColor, int fontSize, Object content, int x, int y) { writeImageString( new File(src), new File(dest), fontColor, fontSize, content, x, y); } /** * 修改图片,返回修改后的图片缓冲区(只输出一行文本) * * @param srcFile * 源文件 * @param destFile * 目标文件 * @param x * x坐标 * @param y * y坐标 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageString(File srcFile, File destFile, Object content, int x, int y) { writeImageString(srcFile, destFile, null , 20 , content, x, y); } /** * 修改图片,返回修改后的图片缓冲区(只输出一行文本) * * @param src * 源文件路径 * @param dest * 目标文件路径 * @param x * x坐标 * @param y * y坐标 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageString(String src, String dest, Object content, int x, int y) { writeImageString( new File(src), new File(dest), content, x, y); } /** * 将两张图片合并后输出图片 * * @param fromFile * 合并来自文件 * @param toFile * 合并到文件 * @param destFile * 目标文件 * @param x * x坐标,默认合并到左上角,当x<0时,合并到右下角 * @param y * y坐标,默认合并到左上角,当y<0时,合并到右下角 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageMerge(File fromFile, File toFile, File destFile, int x, int y) { InputStream frominput = null ; InputStream toinput = null ; OutputStream output = null ; try { // 获取源图片输入流from frominput = new BufferedInputStream( new FileInputStream(fromFile)); // 获取源图片输入流to toinput = new BufferedInputStream( new FileInputStream(toFile)); // 获取文件输出流 output = new BufferedOutputStream( new FileOutputStream(destFile)); // 获取from图片缓冲区 BufferedImage fromImg = ImageIO.read(frominput); // 获取to图片缓冲区 BufferedImage toImg = ImageIO.read(toinput); // 获取from图片宽度 int fromWidth = fromImg.getWidth(); // 获取from图片高度 int fromHeight = fromImg.getHeight(); // 获取to图片宽度 int toWidth = toImg.getWidth(); // 获取to图片高度 int toHeight = toImg.getHeight(); // Graphics2D ,Graphics 类,提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。 // 它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。验证码生成可以用到此类。 // public abstract class Graphics2Dextends Graphics 此 Graphics2D 类扩展了 Graphics 类, // 提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。 Graphics2D g = toImg.createGraphics(); // 合并图片 if (x < 0 ) { // 合并到右下角 x = toWidth - fromWidth; } if (y < 0 ) { // 合并到右下角 y = toHeight - fromHeight; } g.drawImage(fromImg, x, y, fromWidth, fromHeight, null ); // 释放资源 g.dispose(); // 获取to文件扩展名 String toExtName = FilenameUtils.getExtension(toFile.getName()); // 输出to图片缓冲区到输出的文件中 ImageIO.write(toImg, toExtName, output); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(frominput); IOUtils.closeQuietly(toinput); IOUtils.closeQuietly(output); } } /** * 将两张图片合并后输出图片 * * @param fromFile * 合并来自文件 * @param toFile * 合并到文件 * @param destFile * 目标文件 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageMerge(File fromFile, File toFile, File destFile) { writeImageMerge(fromFile, toFile, destFile, 0 , 0 ); } /** * 将两张图片合并后输出图片 * * @param from * 合并来自文件路径 * @param to * 合并到文件路径 * @param dest * 目标文件路径 * @param x * x坐标,默认合并到左上角,当x<0时,合并到右下角 * @param y * y坐标,默认合并到左上角,当y<0时,合并到右下角 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageMerge(String from, String to, String dest, int x, int y) { writeImageMerge( new File(from), new File(to), new File(dest), x, y); } /** * 将两张图片合并后输出图片 * * @param from * 合并来自文件路径 * @param to * 合并到文件路径 * @param dest * 目标文件路径 * @author 张军 * @date 2015-11-03 21:59:00 * @modifiyNote * @version 1.0 */ public static void writeImageMerge(String from, String to, String dest) { writeImageMerge( new File(from), new File(to), new File(dest), 0 , 0 ); } } |
本文为张军原创文章,转载无需和我联系,但请注明来自张军的军军小站,个人博客http://www.zhangjunbk.com