繁体   English   中英

在请求spring boot java中识别作为多部分文件出现的相同图像

[英]Identifying the same image in coming as multipart file in request spring boot java

我正在创建一个带有 spring boot 的应用程序,我正在对图像进行 OCR。 我的请求要求提供多部分文件。 当我得到多部分文件时,我需要知道我是否已经处理过相同的图像。

我创建了 MultipartEntity 和相同的哈希。 我相信下次如果出现相同的文件。 我将创建哈希并进行比较。

当我尝试这样做时。 我发现它的哈希值总是不同的。 有没有一种方法可以识别此图像以前是 OCR 的,因此仅基于散列我将检索结果。

Request params as file in request:-
@RequestParam("file") MultipartFile file

这就是我尝试创建哈希的方式:

  FileBody fileBody = new FileBody(new File(fileName));
  MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
  multipartEntity.addPart("file", fileBody);
  String requestBodyHash = PanUtil.getHashFromRequestBody(multipartEntity.toString());

  public static String getHashFromRequestBody(String req) {

    String requestBodyHash = null;
    try {
      requestBodyHash = generateSha2FromPayload(req);
    } catch (NoSuchAlgorithmException | URISyntaxException e) {
      log.error("Exception occured while creating hash from request {}", e);
      throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, e.getMessage());
    }
    return requestBodyHash;
  }

  public static String generateSha2FromPayload(String json)
      throws URISyntaxException, NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] digest = md.digest(json.getBytes(StandardCharsets.UTF_8));
    return toHexString(digest);
  }

我使用下面的代码来查找图像的相同哈希值。

  public static String toHexString(byte[] hash) {

    // Convert byte array into signum representation
    BigInteger number = new BigInteger(1, hash);

    // Convert message digest into hex value
    StringBuilder hexString = new StringBuilder(number.toString(16));

    // Pad with leading zeros
    while (hexString.length() < 32) {
      hexString.insert(0, '0');
    }
    return hexString.toString();
  }

调用这个函数。 这将始终给出相同的十六进制。

toHexString(file.getBytes())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM