Forráskód Böngészése

add image project

Noah Vogt 3 éve
szülő
commit
12d386c082

+ 167 - 0
image-project/ImageInfo.java

@@ -0,0 +1,167 @@
+// package XXX 
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+@SuppressWarnings("all")
+public class ImageInfo {
+    int height; // private
+    int width; // private
+    String mimeType;// private
+
+    private ImageInfo() {}
+
+    public ImageInfo(File file) throws IOException {
+
+        FileInputStream fileInputStream = null;
+
+        try {           
+            fileInputStream = new FileInputStream(file);
+            processStream(fileInputStream);
+        } catch(IOException e){
+            System.out.println("Error: " + e + "\n" + "Program ist sofort beendet !");
+            System.exit(0);
+        } finally {
+            try {
+                if (fileInputStream != null)
+                    fileInputStream.close();
+            } catch (IOException e) {
+                System.out.println("Error: " + e + "\n" + "Program ist sofort beendet !");
+                System.exit(0);
+            }
+        }
+    }
+
+    public ImageInfo(InputStream inputStream) throws IOException {
+        processStream(inputStream);
+    }
+
+    public ImageInfo(byte[] bytes) throws IOException {
+        InputStream inputStream = new ByteArrayInputStream(bytes);
+        try {
+            processStream(inputStream);
+        } finally {
+            inputStream.close();
+        }
+    }
+
+    private void processStream(InputStream inputStream) throws IOException {
+        int c1 = inputStream.read();
+        int c2 = inputStream.read();
+        int c3 = inputStream.read();
+
+        mimeType = null;
+        width = height = -1;
+
+        if (c1 == 'G' && c2 == 'I' && c3 == 'F') { // GIF
+            inputStream.skip(3);
+            width = readInt(inputStream,2,false);
+            height = readInt(inputStream,2,false);
+            mimeType = "image/gif";
+        } else if (c1 == 0xFF && c2 == 0xD8) { // JPG
+            while (c3 == 255) {
+                int marker = inputStream.read();
+                int len = readInt(inputStream,2,true);
+                if (marker == 192 || marker == 193 || marker == 194) {
+                    inputStream.skip(1);
+                    height = readInt(inputStream,2,true);
+                    width = readInt(inputStream,2,true);
+                    mimeType = "image/jpeg";
+                    break;
+                }
+                inputStream.skip(len - 2);
+                c3 = inputStream.read();
+            }
+        } else if (c1 == 137 && c2 == 80 && c3 == 78) { // PNG
+            inputStream.skip(15);
+            width = readInt(inputStream,2,true);
+            inputStream.skip(2);
+            height = readInt(inputStream,2,true);
+            mimeType = "image/png";
+        } else if (c1 == 66 && c2 == 77) { // BMP
+            inputStream.skip(15);
+            width = readInt(inputStream,2,false);
+            inputStream.skip(2);
+            height = readInt(inputStream,2,false);
+            mimeType = "image/bmp";
+        } else {
+            int c4 = inputStream.read();
+            if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
+            || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { //TIFF
+                boolean bigEndian = c1 == 'M';
+                int ifd = 0;
+                int entries;
+                ifd = readInt(inputStream,4,bigEndian);
+                inputStream.skip(ifd - 8);
+                entries = readInt(inputStream,2,bigEndian);
+                for (int i = 1; i <= entries; i++) {
+                    int tag = readInt(inputStream,2,bigEndian);
+                    int fieldType = readInt(inputStream,2,bigEndian);
+                    long count = readInt(inputStream,4,bigEndian);
+                    int valOffset;
+                    if ((fieldType == 3 || fieldType == 8)) {
+                        valOffset = readInt(inputStream,2,bigEndian);
+                        inputStream.skip(2);
+                    } else {
+                        valOffset = readInt(inputStream,4,bigEndian);
+                    }
+                    if (tag == 256) {
+                        width = valOffset;
+                    } else if (tag == 257) {
+                        height = valOffset;
+                    }
+                    if (width != -1 && height != -1) {
+                        mimeType = "image/tiff";
+                        break;
+                    }
+                }
+            }
+        }
+        if (mimeType == null) {
+            throw new IOException("Unsupported image type");
+        }
+    }
+
+    private int readInt(InputStream is, int noOfBytes, boolean bigEndian) throws IOException {
+        int ret = 0;
+        int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0;
+        int cnt = bigEndian ? -8 : 8;
+        for(int i=0;i<noOfBytes;i++) {
+            ret |= is.read() << sv;
+            sv += cnt;
+        }
+        return ret;
+    }
+
+    public int getHeight() {
+        return height;
+    }
+
+    public void setHeight(int height) {
+        this.height = height;
+    }
+
+    public int getWidth() {
+        return width;
+    }
+
+    public void setWidth(int width) {
+        this.width = width;
+    }
+
+    public String getMimeType() {
+        return mimeType;
+    }
+
+    public void setMimeType(String mimeType) {
+        this.mimeType = mimeType;
+    }
+
+    @Override
+    public String toString() {
+        return "MIME Type : " + mimeType + "\t Width : " + width + "\t Height : " + height; 
+    }
+}

BIN
image-project/Images/Doctor_Strange.png


BIN
image-project/Images/Sample_204_255_20_147.png


BIN
image-project/Images/Taj_Mahal3.png


BIN
image-project/Images/Taj_Mahal4.png


+ 34 - 0
image-project/MyImage.java

@@ -0,0 +1,34 @@
+import java.io.File;
+import java.io.IOException;
+import java.awt.image.BufferedImage;
+import javax.imageio.ImageIO;
+
+public class MyImage{
+    public static void main(String args[])throws IOException{
+        int width = 963;    //width of the image
+        int height = 640;   //height of the image
+        BufferedImage image = null;
+        File f = null;
+
+        //read image
+        try {
+            f = new File("Images/image-processing-taj.png"); //image file path
+            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+            image = ImageIO.read(f);
+            System.out.println("Reading complete.");
+        } catch(IOException e){
+            System.out.println("Error: "+e);
+        }
+
+        System.out.println("width = " + image.getWidth() + " height = " + image.getHeight());
+
+        //write image
+        try {
+            f = new File("Images/Output.jpg");  //output file path
+            ImageIO.write(image, "jpg", f);
+            System.out.println("Writing complete.");
+        } catch(IOException e){
+            System.out.println("Error: "+e);
+        }
+    }//main() ends here
+}//class ends here

+ 65 - 0
image-project/MyImageInfo.java

@@ -0,0 +1,65 @@
+import java.io.File;
+import java.io.IOException;
+import java.awt.image.BufferedImage;
+import javax.imageio.ImageIO;
+
+public class MyImageInfo{
+    public static void main(String args[]) throws IOException{
+        BufferedImage image_on_disk = null;
+        //         File fin = new File("Images/Computer3.png"),
+        //         fout = new File("Images/Computer3.jpg"); // input/output file paths
+        File fin = new File("Images/Taj_Mahal3.png"), // input file path
+        fout = new File("Images/Taj_Mahal3.jpg"); // output file path
+
+        ImageInfo imageInfo = new ImageInfo(fin);
+        System.out.println("Informationen zum input-File " + fin + ":\n" + imageInfo + "\n");
+
+        int width = imageInfo.width;     // width of the image
+        int height = imageInfo.height;   // height of the image
+
+        image_on_disk = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+
+        //read image
+        try {
+            image_on_disk = ImageIO.read(fin);
+            System.out.println("Reading " + fin + " complete !");
+        } catch(IOException e){
+            System.out.println("Error: " + e);
+        }
+
+        //write image
+        try {
+            ImageIO.write(image_on_disk, "jpg", fout);
+            System.out.println("Writing " + fout + " complete !");
+        } catch(IOException e){
+            System.out.println("Error: " + e);
+        }
+    }//main() ends here
+}//class ends here
+
+// BufferedImage bufferedImage;
+// 
+// 	try {
+// 
+// 	  //read image file
+// 	  bufferedImage = ImageIO.read(new File("c:\\javanullpointer.png"));
+// 
+// 	  // create a blank, RGB, same width and height, and a white background
+// 	  BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
+// 			bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
+// 	  newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
+// 
+// 	  // write to jpeg file
+// 	  ImageIO.write(newBufferedImage, "jpg", new File("c:\\javanullpointer.jpg"));
+// 
+// 	  System.out.println("Done");
+// 
+// 	} catch (IOException e) {
+// 
+// 	  e.printStackTrace();
+// 
+// 	}
+// 
+//    }
+// 
+// }

+ 124 - 0
image-project/MyImage_ohne_A_mit_A.java

@@ -0,0 +1,124 @@
+import java.awt.Color;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import javax.imageio.ImageIO;
+import flanagan.io.Db;
+import flanagan.io.FileNameSelector;
+import java.util.regex.Pattern;
+
+public class MyImage_ohne_A_mit_A {
+
+    public static void main(String [] args) {
+
+        /**
+         * 1. Geben Sie den Namen des Input-Files an.
+         * 2. Geben Sie den Namen des Output-Files an
+         * 
+         * @author (your name) 
+         * @version (a version number or a date)
+         */
+
+        boolean answer = false, file_exist = false;
+        String inFileName, inFileNamePfad, outFileName, outFileNamePfad, outFormat = "";
+
+        FileNameSelector fc = new FileNameSelector("Images/");
+        //         fc.setPath("Images/");
+
+        File fin = null, fout = null;            
+
+        while (!answer && !file_exist) {       
+            inFileName = fc.selectFile("Wähle das Input-File:");
+            //         System.out.println(fc.getPathName());
+            inFileNamePfad = "Images/" + inFileName;
+            //             System.out.println("inFileNamePfad = " + inFileNamePfad);
+
+            fin = new File(inFileNamePfad); // input file path
+
+            String [] inFileName_Teil = inFileName.split(Pattern.quote(".")); // Ermitteln den Stamm- und die Erweiterung-Teile von inFileName:
+            // inFileName_Teil[0] = Stammname, inFileName_Teil[1] = Erweiterung (png, jpg, ...)
+
+            outFileName = Db.readLine("Geben Sie den Namen des Output-Files MIT einer Erweiterung (png, jpg, etc.) ein" + "\n" + 
+                "und drücken die ENTER-Taste für Bestätigung!", inFileName_Teil[0] + ".jpg");
+
+            String [] outFileName_Teil = outFileName.split(Pattern.quote(".")); // Ermitteln den Stamm- und die Erweiterung-Teile von outFileName:
+            // outFileName_Teil[0] = Stammname, outFileName_Teil[1] = Erweiterung (png, jpg, ...)
+            outFileNamePfad = "Images/" + outFileName;
+
+            outFormat = outFileName_Teil[1].trim();
+
+            fout = new File(outFileNamePfad); // output file path
+
+            if (inFileNamePfad.equals(outFileNamePfad)) { 
+                file_exist = Db.noYes("Die Input- und Output-Bilddateien haben die gleichen Namen!" + "\n" +
+                    "Möchten Sie trotzdem fortsetzen?");
+
+                if (!file_exist) {
+                    //                     file_exist = false;
+                    System.out.println("file_exist = " + file_exist + " answer = " + answer);
+                    continue;
+                }
+
+            }
+
+            answer = Db.yesNo("Die Angaben lauten:" + "\n" +
+                "Input-Bilddatei: " + "\"" + inFileNamePfad + "\"" + "\n" +
+                "Output-Bilddatei: " + "\"" + outFileNamePfad + "\"" + "\n" +
+                "Die Ertweiterung des Input-Files: " + "\"" + inFileName_Teil[1] + "\"" + "\n" +
+                "Die Ertweiterung des Output-Files: " + "\"" + outFileName_Teil[1] + "\"" + "\n\n" +
+                "Sind Sie damit einverstanden?");
+        }
+
+        BufferedImage image_on_disk = null, image_memory = null;
+        boolean Alpha;
+
+        //******************** Image einlesen ************************
+
+        try {
+            image_on_disk = ImageIO.read(fin);
+            System.out.println("Reading " + fin + " complete !"); } // end try
+        catch(IOException e){
+            System.out.println("Error: " + e); } // end catch
+
+        int width =  image_on_disk.getWidth(); // Breite des Images
+        int height =  image_on_disk.getHeight(); // Höhe des Images  
+
+        System.out.println("\n" + "width = " + width + " height = " + height + "\n");
+
+        if (image_on_disk.getColorModel().hasAlpha()) {
+            Alpha = true;
+            System.out.println("Die Datei HAT Alpha-Kanal\n"); // image_on_disk hat A-Kanal
+        } else {
+            Alpha = false;
+            System.out.println("Die Datei hat KEINEN Alpha-Kanal\n"); // image_on_disk hat keinen A-Kanal
+        }
+
+        //******************** Image kreieren und anpassen **********************
+
+        if (outFormat.equals("png") && Alpha) { // png4 <-> png4
+            //             System.out.println("Wir sind da: 1\n");
+            image_memory = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // Typ TYPE_INT_ARGB, 4 Kanäle: ARGB,d.h. MIT Alpha-Kanal
+
+            // Kreieren das GLEICHE 4-Kanal Image wie image_on_disk und mit Aufrechtserhaltung des Alpha-Kanals
+            image_memory.createGraphics().drawImage(image_on_disk, 0, 0, null);
+        }
+        else  { // png4/3 -> jpg oder png3 -> png3 oder jpg -> png3
+            //             System.out.println("Wir sind da: 2\n");
+            image_memory = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Typ TYPE_INT_RGB, 3 Kanäle: RGB, d.h. OHNE Alpha-Kanal
+
+            // Kreieren ein Image mit der gleichen Breite and Höhe wie image_on_disk und WEISSEM Hintergrund zum Erzeten des A-Kanal
+            image_memory.createGraphics().drawImage(image_on_disk, 0, 0, Color.WHITE, null);
+        }
+
+        //******************** Image schreiben ************************
+
+        try {
+            ImageIO.write(image_memory, outFormat, fout);
+            System.out.println("Writing " + fout + " complete !"); } // end try 
+        catch(IOException e) {
+            System.out.println("Error: " + e); } // end catch
+
+        System.exit(1); // Programm beenden
+
+    } // end main
+} // end class MyImage_ohne_A_mit_A

BIN
image-project/Projekt_Image.pdf


+ 20 - 0
image-project/path-setup

@@ -0,0 +1,20 @@
+#!/bin/bash
+
+# setup JAVACLASS path for using the flanagan library
+
+# quit before potential errors
+if [ ! -f "$PWD/lib/flanagan.jar" ]; then
+    echo "Error: Please put your 'flanagan.jar' in ./lib/flanagan.jar" 1>&2
+    exit 1
+fi
+if [ ! -f "$PWD/lib/stdlib.jar" ]; then
+    echo "Error: Please put your 'stdlib.jar' in ./lib/stdlib.jar" 1>&2
+    exit 1
+fi
+if ! echo "${PWD##*/}" | grep -q "^image-project"; then
+    echo "Error: Please execute this file in the 'image-project' directory" 1>&2
+    exit 1
+fi
+
+echo "export CLASSPATH=\":$PWD/lib/flanagan.jar:$PWD/lib/stdlib.jar:\"" > .path
+# now execute in your commandline "source .path"