예제 #1
0
  private void parseImage(Image image, File file) throws Exception {
    try {
      // Detects the file type
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputStream = new FileInputStream(file);
      ParseContext parseContext = new ParseContext();

      // Parser
      AutoDetectParser parser = new AutoDetectParser();
      parser.parse(inputStream, handler, metadata, parseContext);

      // Image field setting
      String date;
      if (metadata.getDate(metadata.ORIGINAL_DATE) != null) {
        date = metadata.getDate(metadata.ORIGINAL_DATE).toString();
      } else if (metadata.getDate(TikaCoreProperties.CREATED) != null) {
        date = metadata.getDate(TikaCoreProperties.CREATED).toString();
      } else if (metadata.getDate(DublinCore.CREATED) != null) {
        date = metadata.getDate(DublinCore.CREATED).toString();
      } else if (metadata.getDate(TikaCoreProperties.METADATA_DATE) != null) {
        date = metadata.getDate(TikaCoreProperties.METADATA_DATE).toString();
      } else if (metadata.getDate(DublinCore.MODIFIED) != null) {
        date = metadata.getDate(DublinCore.MODIFIED).toString();
      } else {
        // Current date+time
        metadata.set(Metadata.DATE, new Date());
        date = metadata.get(Metadata.DATE);
      }
      image.setLongitude(metadata.get(Geographic.LONGITUDE));
      image.setLatitude(metadata.get(Geographic.LATITUDE));
      ImageOperations.setMetadataParsingFinished();

      if (date != null) {
        image.setDate(date.toString());
      } else {
        image.setDate(null);
      }
      image.setLongitude(image.getLongitude());
      image.setLatitude(image.getLatitude());
      aPII.reverseGeocode(image);
      ImageOperations.setReverseGeocodeFinished();
      ImageOperations iO = new ImageOperations();
      iO.doOCR(image, file);
      ImageOperations.setOcrFinished();

    } catch (IOException e) {
      System.out.println(e.getMessage());
    } catch (TikaException te) {
      System.out.println(te.getMessage());
    } catch (SAXException se) {
      System.out.println(se.getMessage());
    } catch (InterruptedException ie) {
      System.out.println(ie.getMessage());
    } catch (IM4JavaException je) {
      je.printStackTrace();
    }
  }
  /**
   * 根据尺寸缩放图片[等比例缩放:参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放]
   *
   * @param imagePath 源图片路径
   * @param newPath 处理后图片路径
   * @param width 缩放后的图片宽度
   * @param height 缩放后的图片高度
   * @return 返回true说明缩放成功,否则失败
   */
  public static boolean zoomImage(
      String imagePath, String newPath, Integer width, Integer height, String quality) {
    boolean flag = false;
    try {
      IMOperation op = new IMOperation();
      op.addImage(imagePath);
      if (width == null) { // 根据高度缩放图片
        op.resize(null, height);
      } else if (height == null) { // 根据宽度缩放图片
        op.resize(width, null);
      } else {
        op.resize(width, height);
      }
      op.addImage(newPath);
      if ((quality != null && quality.equals(""))) {
        op.addRawArgs("-quality", quality);
      }
      ConvertCmd convert = new ConvertCmd(true);
      if (OS_NAME.indexOf("win") >= 0) {
        // linux下不要设置此值,不然会报错
        String path = getGraphicsMagickPath();
        if (StringUtils.isNotBlank(path)) convert.setSearchPath(path);
      }
      convert.run(op);
      flag = true;
    } catch (IOException e) {
      flag = false;
      e.printStackTrace();
    } catch (InterruptedException e) {
      flag = false;
      e.printStackTrace();
    } catch (IM4JavaException e) {
      flag = false;
      e.printStackTrace();
    } finally {

    }
    return flag;
  }
예제 #3
0
  public static void addWatermark(File srcFile, File destFile, File watermarkFile, int alpha) {
    Assert.notNull(srcFile);
    Assert.notNull(destFile);
    Assert.state(alpha >= 0);
    Assert.state(alpha <= 100);
    if (watermarkFile == null || !watermarkFile.exists()) {
      try {
        FileUtils.copyFile(srcFile, destFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
      return;
    }
    if (type == Type.jdk) {
      Graphics2D graphics2D = null;
      ImageOutputStream imageOutputStream = null;
      ImageWriter imageWriter = null;
      try {
        BufferedImage srcBufferedImage = ImageIO.read(srcFile);
        int srcWidth = srcBufferedImage.getWidth();
        int srcHeight = srcBufferedImage.getHeight();
        BufferedImage destBufferedImage =
            new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
        graphics2D = destBufferedImage.createGraphics();
        graphics2D.setBackground(BACKGROUND_COLOR);
        graphics2D.clearRect(0, 0, srcWidth, srcHeight);
        graphics2D.drawImage(srcBufferedImage, 0, 0, null);
        graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100F));

        BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile);
        int watermarkImageWidth = watermarkBufferedImage.getWidth();
        int watermarkImageHeight = watermarkBufferedImage.getHeight();
        int x = srcWidth - watermarkImageWidth;
        int y = srcHeight - watermarkImageHeight;

        graphics2D.drawImage(
            watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, null);

        imageOutputStream = ImageIO.createImageOutputStream(destFile);
        imageWriter =
            ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName()))
                .next();
        imageWriter.setOutput(imageOutputStream);
        ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
        imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        imageWriteParam.setCompressionQuality(DEST_QUALITY / 100F);
        imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam);
        imageOutputStream.flush();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (graphics2D != null) {
          graphics2D.dispose();
        }
        if (imageWriter != null) {
          imageWriter.dispose();
        }
        if (imageOutputStream != null) {
          try {
            imageOutputStream.close();
          } catch (IOException e) {
          }
        }
      }
    } else {
      IMOperation operation = new IMOperation();
      operation.dissolve(alpha);
      operation.quality((double) DEST_QUALITY);
      operation.addImage(watermarkFile.getPath());
      operation.addImage(srcFile.getPath());
      operation.addImage(destFile.getPath());
      if (type == Type.graphicsMagick) {
        CompositeCmd compositeCmd = new CompositeCmd(true);
        if (graphicsMagickPath != null) {
          compositeCmd.setSearchPath(graphicsMagickPath);
        }
        try {
          compositeCmd.run(operation);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IM4JavaException e) {
          e.printStackTrace();
        }
      } else {
        CompositeCmd compositeCmd = new CompositeCmd(false);
        if (imageMagickPath != null) {
          compositeCmd.setSearchPath(imageMagickPath);
        }
        try {
          compositeCmd.run(operation);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IM4JavaException e) {
          e.printStackTrace();
        }
      }
    }
  }
예제 #4
0
  public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) {
    Assert.notNull(srcFile);
    Assert.notNull(destFile);
    Assert.state(destWidth > 0);
    Assert.state(destHeight > 0);
    if (type == Type.jdk) {
      Graphics2D graphics2D = null;
      ImageOutputStream imageOutputStream = null;
      ImageWriter imageWriter = null;
      try {
        BufferedImage srcBufferedImage = ImageIO.read(srcFile);
        int srcWidth = srcBufferedImage.getWidth();
        int srcHeight = srcBufferedImage.getHeight();
        int width = destWidth;
        int height = destHeight;
        if (srcHeight >= srcWidth) {
          width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
        } else {
          height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
        }
        BufferedImage destBufferedImage =
            new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
        graphics2D = destBufferedImage.createGraphics();
        graphics2D.setBackground(BACKGROUND_COLOR);
        graphics2D.clearRect(0, 0, destWidth, destHeight);
        graphics2D.drawImage(
            srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
            (destWidth / 2) - (width / 2),
            (destHeight / 2) - (height / 2),
            null);

        imageOutputStream = ImageIO.createImageOutputStream(destFile);
        imageWriter =
            ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName()))
                .next();
        imageWriter.setOutput(imageOutputStream);
        ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
        imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0));
        imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam);
        imageOutputStream.flush();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (graphics2D != null) {
          graphics2D.dispose();
        }
        if (imageWriter != null) {
          imageWriter.dispose();
        }
        if (imageOutputStream != null) {
          try {
            imageOutputStream.close();
          } catch (IOException e) {
          }
        }
      }
    } else {
      IMOperation operation = new IMOperation();
      operation.thumbnail(destWidth, destHeight);
      operation.gravity("center");
      operation.background(toHexEncoding(BACKGROUND_COLOR));
      operation.extent(destWidth, destHeight);
      operation.quality((double) DEST_QUALITY);
      operation.addImage(srcFile.getPath());
      operation.addImage(destFile.getPath());
      if (type == Type.graphicsMagick) {
        ConvertCmd convertCmd = new ConvertCmd(true);
        if (graphicsMagickPath != null) {
          convertCmd.setSearchPath(graphicsMagickPath);
        }
        try {
          convertCmd.run(operation);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IM4JavaException e) {
          e.printStackTrace();
        }
      } else {
        ConvertCmd convertCmd = new ConvertCmd(false);
        if (imageMagickPath != null) {
          convertCmd.setSearchPath(imageMagickPath);
        }
        try {
          convertCmd.run(operation);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IM4JavaException e) {
          e.printStackTrace();
        }
      }
    }
  }