Class ImageUtil

java.lang.Object
cn.net.pap.common.opencv.ImageUtil

public class ImageUtil extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    generateEmptyJpeg() 的返回值 Base64.getEncoder().encodeToString(bytes); Base64.getDecoder().decode(s) response.getOutputStream().write(java.util.Base64.getDecoder().decode(oneXOneJpg));
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    base64ToImage(String base64String, String outputFilePath)
    base64ToImage
    cover(BufferedImage originalImage, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int targetX, int targetY)
    从一个区域获得信息覆盖到另一个区域
    createImageWithRegions(int width, int height, Color backColor, int[][] regions)
    创建一个指定宽高的图像,并在指定区域内添加不同的颜色
    static Boolean
    cropImageCut(String inputFilePath, String outputFilePath, int x, int y, int width, int height)
    高效的图像裁剪功能, 指定原始图像路径和裁剪后的图像路径, 指定左上角的x/y 和 宽高 进行高效的裁剪 相比于 ImageIO.read("").getSubimage(x, y, width, height), 在极端情况下能够提示执行效率10倍以上。 实验过程中一张7.35M大小的JPG图像(分辨率 9007 x 6221),当前方法执行时间小于1s, ImageIO.read("").getSubimage 执行时间24s.
    static List<String>
    cropImageCutList(String inputFilePath, List<Rectangle> regions)
    在一张图像中同时截取多个矩形区域并按顺序返回 base64.
    static byte[]
    创建1x1像素的最小JPEG图像 背景色白色 的 jpg 图像字符串, 可以直接 response.getOutputStream().write(generateEmptyJpeg()); 写入响应流
    static boolean
    horizontalMirror(String inputFilePath, String outputFilePath)
    水平镜像
    mergeByPointInTwoPic(BufferedImage leftImage, int leftPoint1X, int leftPoint1Y, int leftPoint2X, int leftPoint2Y, BufferedImage rightImage, int rightPoint1X, int rightPoint1Y, int rightPoint2X, int rightPoint2Y)
    根据指定坐标合并两张图像
    mergeImages(String img1Path, String img2Path, int x1, int y1, int x2, int y2)
    将两张图片根据给定的坐标点进行拼接
    static void
    顺时针旋转图像90度并覆盖原图
    static boolean
    rotateImage(String inputFilePath, String outputFilePath, double angle)
    图像旋转
    static boolean
    scaleAndGray(String inputPath, String outputPath, Integer widthHeight)
    图像等比缩放并灰度化
    static boolean
    verticalMirror(String inputFilePath, String outputFilePath)
    垂直镜像

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • oneXOneJpg

      public static final String oneXOneJpg
      generateEmptyJpeg() 的返回值 Base64.getEncoder().encodeToString(bytes); Base64.getDecoder().decode(s) response.getOutputStream().write(java.util.Base64.getDecoder().decode(oneXOneJpg));
      See Also:
  • Constructor Details

    • ImageUtil

      public ImageUtil()
  • Method Details

    • generateEmptyJpeg

      public static byte[] generateEmptyJpeg()
      创建1x1像素的最小JPEG图像 背景色白色 的 jpg 图像字符串, 可以直接 response.getOutputStream().write(generateEmptyJpeg()); 写入响应流
      Returns:
    • cropImageCut

      public static Boolean cropImageCut(String inputFilePath, String outputFilePath, int x, int y, int width, int height)
      高效的图像裁剪功能, 指定原始图像路径和裁剪后的图像路径, 指定左上角的x/y 和 宽高 进行高效的裁剪 相比于 ImageIO.read("").getSubimage(x, y, width, height), 在极端情况下能够提示执行效率10倍以上。 实验过程中一张7.35M大小的JPG图像(分辨率 9007 x 6221),当前方法执行时间小于1s, ImageIO.read("").getSubimage 执行时间24s.
      Parameters:
      inputFilePath -
      outputFilePath -
      x -
      y -
      width -
      height -
      Returns:
    • cropImageCutList

      public static List<String> cropImageCutList(String inputFilePath, List<Rectangle> regions)
      在一张图像中同时截取多个矩形区域并按顺序返回 base64.
      Parameters:
      inputFilePath -
      regions -
      Returns:
    • scaleAndGray

      public static boolean scaleAndGray(String inputPath, String outputPath, Integer widthHeight)
      图像等比缩放并灰度化
      Parameters:
      inputPath -
      outputPath -
      widthHeight -
      Returns:
    • mergeImages

      public static BufferedImage mergeImages(String img1Path, String img2Path, int x1, int y1, int x2, int y2) throws IOException
      将两张图片根据给定的坐标点进行拼接
      Parameters:
      img1Path - 第一张图片的路径
      img2Path - 第二张图片的路径
      x1 - 第一张图片放置的x坐标
      y1 - 第一张图片放置的y坐标
      x2 - 第二张图片放置的x坐标
      y2 - 第二张图片放置的y坐标
      Returns:
      拼接后的图片
      Throws:
      IOException
    • rotateImage

      public static boolean rotateImage(String inputFilePath, String outputFilePath, double angle)
      图像旋转
      Parameters:
      inputFilePath -
      outputFilePath -
      angle -
      Returns:
    • horizontalMirror

      public static boolean horizontalMirror(String inputFilePath, String outputFilePath) throws Exception
      水平镜像
      Returns:
      Throws:
      Exception
    • verticalMirror

      public static boolean verticalMirror(String inputFilePath, String outputFilePath)
      垂直镜像
      Parameters:
      inputFilePath -
      outputFilePath -
      Returns:
    • cover

      public static BufferedImage cover(BufferedImage originalImage, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int targetX, int targetY)
      从一个区域获得信息覆盖到另一个区域
      Parameters:
      originalImage -
      sourceX -
      sourceY -
      sourceWidth -
      sourceHeight -
      targetX -
      targetY -
      Returns:
    • mergeByPointInTwoPic

      public static BufferedImage mergeByPointInTwoPic(BufferedImage leftImage, int leftPoint1X, int leftPoint1Y, int leftPoint2X, int leftPoint2Y, BufferedImage rightImage, int rightPoint1X, int rightPoint1Y, int rightPoint2X, int rightPoint2Y)
      根据指定坐标合并两张图像
      Parameters:
      leftImage - 左图像
      leftPoint1X - 左图像上边点 X 坐标
      leftPoint1Y - 左图像上边点 Y 坐标
      leftPoint2X - 左图像下边点 X 坐标
      leftPoint2Y - 左图像下边点 Y 坐标
      rightImage - 右图像
      rightPoint1X - 右图像上边点 X 坐标
      rightPoint1Y - 右图像上边点 Y 坐标
      rightPoint2X - 右图像下边点 X 坐标
      rightPoint2Y - 右图像下边点 Y 坐标
      Returns:
    • createImageWithRegions

      public static BufferedImage createImageWithRegions(int width, int height, Color backColor, int[][] regions)
      创建一个指定宽高的图像,并在指定区域内添加不同的颜色
      Parameters:
      width - 图像的宽度
      height - 图像的高度
      regions - 一个二维数组,每个子数组包含四个元素:x, y, width, height(区域的左上角坐标和宽高) 以及第五个元素:颜色值(例如:Color.RED.getRGB())
      Returns:
      创建的BufferedImage对象
    • base64ToImage

      public static void base64ToImage(String base64String, String outputFilePath) throws IOException
      base64ToImage
      Parameters:
      base64String -
      outputFilePath -
      Throws:
      IOException
    • rotate90ClockwiseAndOverwrite

      public static void rotate90ClockwiseAndOverwrite(File imageFile) throws IOException
      顺时针旋转图像90度并覆盖原图
      Parameters:
      imageFile - 图像文件
      Throws:
      IOException - 如果处理失败