ulrich
2022-02-11 9976d833227e6d1d25559de2bae56f5f3cf78150
src/de/uhilger/bildhelfer/Verkleinerer.java
@@ -29,14 +29,14 @@
    int groesse = getIntFromArg(args[1], 1200);
    float qualitaet = getFloatFromArg(args[2], (float) 0.7);
    File eingangsOrdner = getFileFromArg(args[3]);
    if(eingangsOrdner == null) {
    if (eingangsOrdner == null) {
      logger.info("Eingangsordner konnte nicht bestimmt werden, Abbruch.");
      return;
    } else {
      logger.info("Eingangsordner " + eingangsOrdner);
    }
    File ausgabeOrdner = getFileFromArg(args[4]);
    if(ausgabeOrdner == null) {
    if (ausgabeOrdner == null) {
      logger.info("Ausgabeordner konnte nicht bestimmt werden, Abbruch.");
      return;
    } else {
@@ -45,23 +45,23 @@
    dateienVerarbeiten(groesse, qualitaet, eingangsOrdner.getAbsolutePath(), ausgabeOrdner.getAbsolutePath());
  }
  private int getIntFromArg(String arg, int defaultValue) {
  public int getIntFromArg(String arg, int defaultValue) {
    try {
      int argValue = Integer.parseInt(arg);
      logger.info("Ausgabegroesse " + argValue);
      return argValue;
    } catch(Exception ex) {
    } catch (Exception ex) {
      logger.info("Ausgabegroesse konnte nicht ermittelt werden, es wird der Standardwert " + defaultValue + " verwendet.");
      return defaultValue;
    }
  }
  private float getFloatFromArg(String arg, float defaultValue) {
  public float getFloatFromArg(String arg, float defaultValue) {
    try {
      float argValue = Float.parseFloat(arg);
      logger.info("Ausgabequalitaet " + argValue);
      return argValue;
    } catch(Exception ex) {
    } catch (Exception ex) {
      logger.info("Ausgabequalitaet konnte nicht ermittelt werden, es wird der Standardwert " + defaultValue + " verwendet.");
      return defaultValue;
    }
@@ -70,27 +70,26 @@
  private File getFileFromArg(String arg) {
    try {
      File f = new File(arg);
      if(!f.exists()) {
      if (!f.exists()) {
        return null;
      } else {
        return f;
      }
    } catch(Exception ex) {
    } catch (Exception ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
      return null;
    }
  }
  private void dateienVerarbeiten(int gr, float quality, String inDirName, String outDirName) {
/*
  public void dateienVerarbeiten(int gr, float quality, String inDirName, String outDirName) {
    /*
   Java-Code zum Verkleinern von Bildern
   args[0] - Anzahl Pixel an der laengsten Kante
   args[1] - qualitaet JPEG, z.B. 0.75 fuer 75% des Originals
   args[1] - qualitaet, z.B. 0.75 fuer 75% des Originals
   args[2] - Eingangsordner
   args[3] - Ausgabeordner
*/
     */
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    //int width = Integer.parseInt(args[0]);
@@ -100,125 +99,121 @@
    //String outDirName = args[3];
    File inFile = new File(inDirName);
    File[] fileList = inFile.listFiles();
    if(fileList != null && fileList.length > 0) {
      for(int i = 0; i < fileList.length; i++) {
    if (fileList != null && fileList.length > 0) {
      for (int i = 0; i < fileList.length; i++) {
        //System.out.println(fileList[i].getAbsolutePath());
        logger.fine(fileList[i].getAbsolutePath());
        File outDir = new File(outDirName);
        File outFile = new File(outDir, fileList[i].getName());
        //System.out.println(outFile.getAbsolutePath());
        logger.info(outFile.getAbsolutePath());
        String fname = fileList[i].getName().toLowerCase();
        try {
          Image image = ImageIO.read(fileList[i]);
          MediaTracker mediaTracker = new MediaTracker(new Container());
          mediaTracker.addImage(image, 0);
          mediaTracker.waitForID(0);
          if (!mediaTracker.isErrorAny()) {
          if (fileList[i].isFile() && (fname.endsWith("jpg") || fname.endsWith("jpeg") || fname.endsWith("png"))) {
            Image image = ImageIO.read(fileList[i]);
            MediaTracker mediaTracker = new MediaTracker(new Container());
            mediaTracker.addImage(image, 0);
            mediaTracker.waitForID(0);
            if (!mediaTracker.isErrorAny()) {
            //float quality = Float.parseFloat(args[1]);
            float factor = (float) 0.0;
              BufferedImage thumbImage;
              int imageWidth = image.getWidth(null);
              int imageHeight = image.getHeight(null);
              int thumbWidth = width;
              int thumbHeight = height;
              if (imageWidth < width) {
                thumbWidth = imageWidth;
              }
              if (imageHeight < height) {
                thumbHeight = imageHeight;
              }
              double thumbRatio = (double) thumbWidth / (double) thumbHeight;
              double imageRatio = (double) imageWidth / (double) imageHeight;
              if (thumbRatio < imageRatio) {
                thumbHeight = (int) (thumbWidth / imageRatio);
              } else {
                thumbWidth = (int) (thumbHeight * imageRatio);
              }
            BufferedImage thumbImage;
            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);
            int thumbWidth = width;
            int thumbHeight = height;
            if(imageWidth < width) {
              thumbWidth = imageWidth;
              String mimeType = fileNameMap.getContentTypeFor("file://" + fileList[i].getAbsolutePath());
              // draw original image to thumbnail image object and
              // scale it to the new size on-the-fly
              if (mimeType.contains("jpeg") || mimeType.contains("png")) {
                thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
              } else {
                thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
              }
              Graphics2D graphics2D = thumbImage.createGraphics();
              graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
              graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
              //float quality = Float.parseFloat(args[1]);
              float factor = (float) 0.0;
              // 30.7.2007: sharpening hinzugefuegt (Anfang)
              //float factor = -0.15f; // minus = sharpen, plus = soften
              //float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
              // 30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400
              //if(thumbWidth < 400 || thumbHeight < 400) {
              //factor = 0.1f;
              //}
              if (factor != (float) 0.0) {
                //float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0};
                float[] array = new float[9];
                array[0] = 0;
                array[1] = factor;
                array[2] = 0;
                array[3] = factor;
                array[4] = 1 - (factor * 4);
                array[5] = factor;
                array[6] = 0;
                array[7] = factor;
                array[8] = 0;
                Kernel kernel = new Kernel(3, 3, array);
                ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
                thumbImage = cOp.filter(thumbImage, null);
              }
              // 30.7.2007: sharpening hinzugefuegt (Ende)
              String imgType;
              if (mimeType.contains("jpg")) {
                imgType = "jpg";
              } else if (mimeType.contains("jpeg")) {
                imgType = "jpg";
              } else if (mimeType.contains("png")) {
                imgType = "png";
              } else if (mimeType.contains("gif")) {
                imgType = "gif";
              } else {
                imgType = "jpg";
              }
              // 14.1.2018: Ausgabe um Qualitaetsparameter erweitert   Beginn
              ImageWriter writer = ImageIO.getImageWritersByFormatName(imgType).next();
              ImageWriteParam iwp = writer.getDefaultWriteParam();
              iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
              iwp.setCompressionQuality(quality);
              writer.setOutput(new FileImageOutputStream(outFile));
              writer.write(null, new IIOImage(thumbImage, null, null), iwp);
              writer.dispose();
              // 14.1.2018 Ende
            }
            if(imageHeight < height) {
              thumbHeight = imageHeight;
            }
            double thumbRatio = (double)thumbWidth / (double)thumbHeight;
            double imageRatio = (double)imageWidth / (double)imageHeight;
            if (thumbRatio < imageRatio) {
              thumbHeight = (int)(thumbWidth / imageRatio);
            }
            else {
              thumbWidth = (int)(thumbHeight * imageRatio);
            }
            String mimeType = fileNameMap.getContentTypeFor("file://" + fileList[i].getAbsolutePath());
            // draw original image to thumbnail image object and
            // scale it to the new size on-the-fly
            if(mimeType.contains("jpeg") || mimeType.contains("png")) {
              thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
            } else {
              thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
            }
            Graphics2D graphics2D = thumbImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
            // 30.7.2007: sharpening hinzugefuegt (Anfang)
            //float factor = -0.15f; // minus = sharpen, plus = soften
            //float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
        /*
          30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400
        */
        /*if(thumbWidth < 400 || thumbHeight < 400) {
          factor = 0.1f;
        }*/
            if(factor != (float) 0.0) {
              //float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0};
              float[] array = new float[9];
              array[0] = 0;
              array[1] = factor;
              array[2] = 0;
              array[3] = factor;
              array[4] = 1-(factor*4);
              array[5] = factor;
              array[6] = 0;
              array[7] = factor;
              array[8] = 0;
              Kernel kernel = new Kernel(3, 3, array);
              ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
              thumbImage = cOp.filter(thumbImage, null);
            }
            // 30.7.2007: sharpening hinzugefuegt (Ende)
            String imgType;
            if(mimeType.contains("jpg")) {
              imgType = "jpg";
            } else if(mimeType.contains("jpeg")) {
              imgType = "jpg";
            } else if(mimeType.contains("png")) {
              imgType = "png";
            } else if(mimeType.contains("gif")) {
              imgType = "gif";
            } else {
              imgType = "jpg";
            }
            /*
               14.1.2018: Ausgabe um Qualitaetsparameter erweitert
               Beginn
            */
            ImageWriter writer = ImageIO.getImageWritersByFormatName(imgType).next();
            ImageWriteParam iwp = writer.getDefaultWriteParam();
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwp.setCompressionQuality(quality);
            writer.setOutput(new FileImageOutputStream(outFile));
            writer.write(null, new IIOImage(thumbImage, null, null), iwp);
            writer.dispose();
            /* 14.1.2018 Ende */
          }
        } catch (InterruptedException ex) {
          //System.out.println("Error: " + ex.getLocalizedMessage());
          logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        } catch (FileNotFoundException e) {
          e.printStackTrace();
          logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } catch (IOException e) {
          e.printStackTrace();
          logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
      }
    } else {
      //System.out.println("fileList is null or empty");
      logger.info("fileList is null or empty");
    }
  }
}