Quellcode
- import java.io.*;
- import java.util.Scanner;
- import java.util.Formatter;
- /**
- *
- * @author Torben Brodt
- * @version 1.0
- *
- * <p />Liest Datei A - und schreibt in B
- */
- public class MakeBinPicture {
- /**
- * @param args
- * args[0] -> Quelle
- * args[1] -> Ziel
- * args[2] -> Grauwert (threshold)
- */
- public static void main(String[] args) throws IOException {
- System.out.println("Beginn");
- //Fehlerabfragen
- assert (args.length == 3) :
- "Ungültige Eingabe";
- String source = args[0], dest = args[1];
- int threshold = Integer.parseInt(args[2]);
- //Versuche die Dateioperationen
- try {
- int count=0;
- Scanner sc1 = new Scanner(new File(source));
- while(sc1.hasNext()) {
- sc1.next();
- count++;
- }
- System.out.println(count+" Integer Werte gez√§hlt");
- sc1.close();
- int[] a = new int[count];
- int j=0;
- String[] header = new String[5];
- // Öffne Quelldatei um den Inhalt zu lesen
- Scanner sc2 = new Scanner(new File(source));
- //sc2.useLocale();
- for(int i=0; i<count-1; i++) {
- if(i>4) {
- j++;
- a[j] = Integer.parseInt(sc2.next());
- } else {
- header[i] = sc2.next();
- }
- }
- System.out.println("Bild gelesen");
- sc2.close();
- //Aendere schwarz weiß
- threshold(a, threshold);
- System.out.println("Bild manipuliert");
- //Öffne Zieldatei um den verarbeiteten Inhalt zu schreiben
- Formatter output = new Formatter(new File(dest));
- output.format(header[0]+"\n#"+dest+"\n"+header[2]+" "+header[3]+"\n1\n");
- for (int wert : a)
- output.format(wert+"\n");
- output.close();
- System.out.println("Bild geschrieben");
- }
- catch(FileNotFoundException e) {
- System.out.println("Fehler: Quelldatei existiert nicht");
- System.exit(1);
- } /*catch(NumberFormatException e) {
- System.out.println("Fehler: Die Quelldatei enthält darf ausschließlich Integer enthalten");
- System.exit(1);
- } catch(Exception e) {
- System.out.println("Fehler: Ein unbekannter Fehler ist aufgetreten");
- System.exit(1);
- }*/
- }
- /**
- * Aendert werte auf 0 oder 1.. je nachdem wo threshold liegt
- */
- static void threshold(int[] a, int threshold)
- {
- for(int i=0; i<a.length; i++)
- if(a[i] < threshold)
- a[i] = 0;
- else
- a[i] = 1;
- }
- }