裁剪、缩放图片工具类

冷不防 2024-04-17 05:26 161阅读 0赞
  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.geom.AffineTransform;
  4. import java.awt.image.AffineTransformOp;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. /**
  9. * 裁剪、缩放图片工具类
  10. */
  11. public class ImageUtis {
  12. /**
  13. * 缩放图片方法
  14. * @param srcImageFile 要缩放的图片路径
  15. * @param result 缩放后的图片路径
  16. * @param height 目标高度像素
  17. * @param width 目标宽度像素
  18. * @param bb 是否补白
  19. */
  20. public final static void scale(String srcImageFile, String result, int height, int width, boolean bb) {
  21. try {
  22. double ratio = 0.0; // 缩放比例
  23. File f = new File(srcImageFile);
  24. BufferedImage bi = ImageIO.read(f);
  25. Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);//bi.SCALE_SMOOTH 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
  26. // 计算比例
  27. if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
  28. double ratioHeight = (new Integer(height)).doubleValue()/ bi.getHeight();
  29. double ratioWhidth = (new Integer(width)).doubleValue()/ bi.getWidth();
  30. if(ratioHeight>ratioWhidth){
  31. ratio= ratioHeight;
  32. }else{
  33. ratio= ratioWhidth;
  34. }
  35. AffineTransformOp op = new AffineTransformOp(AffineTransform//仿射转换
  36. .getScaleInstance(ratio, ratio), null);//返回表示剪切变换的变换
  37. itemp = op.filter(bi, null);//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
  38. }
  39. if (bb) {//补白
  40. BufferedImage image = new BufferedImage(width, height,
  41. BufferedImage.TYPE_INT_RGB);//构造一个类型为预定义图像类型之一的 BufferedImage。
  42. Graphics2D g = image.createGraphics();//创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中。
  43. g.setColor(Color.white);//控制颜色
  44. g.fillRect(0, 0, width, height);// 使用 Graphics2D 上下文的设置,填充 Shape 的内部区域。
  45. if (width == itemp.getWidth(null))
  46. g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
  47. itemp.getWidth(null), itemp.getHeight(null),
  48. Color.white, null);
  49. else
  50. g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
  51. itemp.getWidth(null), itemp.getHeight(null),
  52. Color.white, null);
  53. g.dispose();
  54. itemp = image;
  55. }
  56. ImageIO.write((BufferedImage) itemp, "JPEG", new File(result)); //输出压缩图片
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. /**
  62. * 裁剪图片方法
  63. * @param bufferedImage 图像源
  64. * @param startX 裁剪开始x坐标
  65. * @param startY 裁剪开始y坐标
  66. * @param endX 裁剪结束x坐标
  67. * @param endY 裁剪结束y坐标
  68. * @return
  69. */
  70. public static BufferedImage cropImage(BufferedImage bufferedImage, int startX, int startY, int endX, int endY) {
  71. int width = bufferedImage.getWidth();
  72. int height = bufferedImage.getHeight();
  73. if (startX == -1) {
  74. startX = 0;
  75. }
  76. if (startY == -1) {
  77. startY = 0;
  78. }
  79. if (endX == -1) {
  80. endX = width - 1;
  81. }
  82. if (endY == -1) {
  83. endY = height - 1;
  84. }
  85. BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
  86. for (int x = startX; x < endX; ++x) {
  87. for (int y = startY; y < endY; ++y) {
  88. int rgb = bufferedImage.getRGB(x, y);
  89. result.setRGB(x - startX, y - startY, rgb);
  90. }
  91. }
  92. return result;
  93. }
  94. }

发表评论

表情:
评论列表 (有 0 条评论,161人围观)

还没有评论,来说两句吧...

相关阅读