| | |
| | | der Klasse Bild |
| | | */ |
| | | |
| | | File inFile = new File(args[1]); |
| | | FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
| | | int width = Integer.parseInt(args[0]); |
| | | int height = width; |
| | | String inDirName = args[1]; |
| | | String outDirName = args[2]; |
| | | File inFile = new File(inDirName); |
| | | File[] fileList = inFile.listFiles(); |
| | | if(fileList != null && fileList.length > 0) { |
| | | for(int i = 0; i < fileList.length; i++) { |
| | | System.out.println(fileList[i].getAbsolutePath()); |
| | | |
| | | File outDir = new File(args[2]); |
| | | File outDir = new File(outDirName); |
| | | File outFile = new File(outDir, fileList[i].getName()); |
| | | System.out.println(outFile.getAbsolutePath()); |
| | | //java.awt.Image image = (java.awt.Image) Toolkit.getDefaultToolkit().getImage(fileList[i].getAbsolutePath()); |
| | | Image image = ImageIO.read(fileList[i]); |
| | | MediaTracker mediaTracker = new MediaTracker(new Container()); |
| | | mediaTracker.addImage(image, 0); |
| | | try { |
| | | mediaTracker.waitForID(0); |
| | | if (!mediaTracker.isErrorAny()) { |
| | | Bild bild = new Bild(); |
| | | bild.writeImageFile(image, Integer.parseInt(args[0]), bild.getMimeType(fileList[i]), new File(outDir, fileList[i].getName()).getAbsolutePath()); |
| | | } |
| | | } catch (InterruptedException ex) { |
| | | System.out.println("Error: " + ex.getLocalizedMessage()); |
| | | } |
| | | |
| | | } |
| | | } else { |
| | | System.out.println("fileList is null or empty"); |
| | | } |
| | | int quality = 90; |
| | | float factor = (float) 0.0; |
| | | |
| | | |
| | | |
| | | /* |
| | | Klasse Bild |
| | | */ |
| | | |
| | | public class Bild { |
| | | |
| | | public static final int WINZIG = 0; |
| | | public static final int KLEIN = 1; |
| | | public static final int SEMI = 2; |
| | | public static final int MITTEL = 3; |
| | | public static final int GROSS = 4; |
| | | |
| | | public String[] GRNAME; // = {"-w", "-k", "-s", "-m", "-g"}; |
| | | public int[] GR; // = {120, 240, 500, 700, 1200}; |
| | | |
| | | public Bild() { |
| | | GRNAME = new String[5] |
| | | GRNAME[0] = "-w"; |
| | | GRNAME[1] = "-k"; |
| | | GRNAME[2] = "-s"; |
| | | GRNAME[3] = "-m"; |
| | | GRNAME[4] = "-g"; |
| | | |
| | | GR = new int[5]; |
| | | GR[0] = 120; |
| | | GR[1] = 240; |
| | | GR[2] = 500; |
| | | GR[3] = 700; |
| | | GR[4] = 1200; |
| | | |
| | | } |
| | | |
| | | public void writeImageStream(Image image, int gr, String mimeType, OutputStream out) throws InterruptedException, IOException { |
| | | ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), out); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param image |
| | | * @param gr |
| | | * @param mimeType |
| | | * @param vName Name der verkleinerten Datei |
| | | * @throws InterruptedException |
| | | * @throws IOException |
| | | */ |
| | | public void writeImageFile(BufferedImage image, int gr, String mimeType, String vName) throws InterruptedException, IOException { |
| | | ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), new File(vName)); |
| | | } |
| | | |
| | | private BufferedImage getReducedImage(Image image, int gr, String mimeType) throws InterruptedException, IOException { |
| | | BufferedImage img; |
| | | int q = 90; |
| | | float sh = (float) 0.0; |
| | | if(mimeType.contains("jpeg")) { |
| | | img = getReducedImageImpl(image, gr, gr, q, sh, false); |
| | | } else { |
| | | img = getReducedImageImpl(image, gr, gr, q, sh, true); |
| | | } |
| | | return img; |
| | | } |
| | | |
| | | /** |
| | | * Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen |
| | | * @param image Ablageort und Name der Bilddatei |
| | | * @param width neue Breite |
| | | * @param height neue hoehe |
| | | * @param quality neue Qualitaet (0 - 100) |
| | | * @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte Unschaerfe, 0.05f fuer leichtes Nachschaerfen) |
| | | * @param withTransparency ob Transparenz benoetigt wird |
| | | * @return neues, in Groesse und Qualitaet entsprechend verringertes Bild |
| | | * @throws java.lang.InterruptedException |
| | | * @throws java.io.FileNotFoundException |
| | | */ |
| | | public BufferedImage getReducedImageImpl(BufferedImage image, int width, int height, int quality, float factor, boolean withTransparency) |
| | | throws InterruptedException, FileNotFoundException, IOException { |
| | | |
| | | BufferedImage thumbImage; |
| | | int imageWidth = image.getWidth(null); |
| | | int imageHeight = image.getHeight(null); |
| | | |
| | | int thumbWidth = width; |
| | | int thumbHeight = height; |
| | | if(imageWidth < width) { |
| | |
| | | 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 |
| | | //BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
| | | BufferedImage thumbImage; |
| | | if(withTransparency) { |
| | | thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
| | | } else { |
| | | 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); |
| | |
| | | // 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 |
| | | */ |
| | |
| | | } |
| | | // 30.7.2007: sharpening hinzugefuegt (Ende) |
| | | |
| | | return thumbImage; |
| | | } |
| | | |
| | | public String imgType(String mimeType) { |
| | | String imgType; |
| | | if(mimeType.contains("jpg")) { |
| | | imgType = "jpg"; |
| | |
| | | } else { |
| | | imgType = "jpg"; |
| | | } |
| | | return imgType; |
| | | |
| | | ImageIO.write(thumbImage, imgType, outFile); |
| | | } |
| | | } catch (InterruptedException ex) { |
| | | System.out.println("Error: " + ex.getLocalizedMessage()); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param v Bild.WINZIG .. Bild.GROSS |
| | | * @return Länge der längsten Kante in Bildpunkten |
| | | */ |
| | | public int getVariantenGroesse(int v) { |
| | | return GR[v]; |
| | | } |
| | | |
| | | public String getVariantenName(int v) { |
| | | return GRNAME[v]; |
| | | } else { |
| | | System.out.println("fileList is null or empty"); |
| | | } |
| | | |
| | | public String getMimeType(File file) { |
| | | String absName = file.getAbsolutePath(); |
| | | FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
| | | return fileNameMap.getContentTypeFor("file://" + absName); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |