Helligkeit eines Bildes berechnen

Allrounder93

Aktives Mitglied
Huhu,

ich erhalte bei dem Code einige Fehler... Das Problem liegt daran, dass ich satt ehemals eine List<List<File>> nun nur noch ein direktes File nutze...

Liegt also an folgdendem Codeschnipsel:
Java:
            List<List<File>> picturesSplit = splitList(pictures, 256);
            ArrayList<Future<List<FileAB>>> futures = new ArrayList<>();
            for(List<File> _picturesChunk: picturesSplit) {
                final List<File> picturesChunk = _picturesChunk;
                futures.add(threadPool.submit(new Callable<List<FileAB>>() {
                    public List<FileAB> call() {
                        List<FileAB> result = calcBrightness(picturesChunk);
                         return result;
                    }
                }));
            }

Kann mir jemand sagen wie ich den Code verbessere?

Java:
package imgaeS;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.imageio.ImageIO;

public class RGB {
	public static void main(String[ ] args){
		File str = new File("D:/Source/Neuer Ordner/Neuer Ordner/20121213.jpg");
		try {
			RGB2(str);
		} catch (FileNotFoundException | InterruptedException | ExecutionException e) {e.printStackTrace();}
	}

	private static void RGB2(File dir) throws InterruptedException, ExecutionException, FileNotFoundException{
		ArrayList<File> pictures = new ArrayList<>();
		pictures.add(dir);
        ArrayList<FileAB> picturesPlusAB = new ArrayList<>();
       
        ExecutorService threadPool = Executors.newFixedThreadPool(8);
       
        {
            List<List<File>> picturesSplit = splitList(pictures, 256);
            ArrayList<Future<List<FileAB>>> futures = new ArrayList<>();
            for(List<File> _picturesChunk: picturesSplit) {
                final List<File> picturesChunk = _picturesChunk;
                futures.add(threadPool.submit(new Callable<List<FileAB>>() {
                    public List<FileAB> call() {
                        List<FileAB> result = calcBrightness(picturesChunk);
                         return result;
                    }
                }));
            }
           
           for(Future<List<FileAB>> future: futures) {
                picturesPlusAB.addAll(future.get());
           }
        }
       
        Collections.sort(picturesPlusAB, new CompRed(0));
           
  
       
        threadPool.shutdown();
        
   
	}
	   
    private static int[] splitRGB(int rgb) {
        return new int[] {(rgb>>>16)&0xFF, (rgb>>>8)&0xFF, rgb&0xFF};
    }
    private static double[] averageBrightness(int[] data) {
        double[] b = {0, 0, 0};
        for(int i = 0; i < data.length; i++) {
            int[] pixel = splitRGB(data[i]);
            b[0] += pixel[0];
            b[1] += pixel[1];
            b[2] += pixel[2];
        }
        b[0] /= data.length;
        b[1] /= data.length;
        b[2] /= data.length;
        return b;
    }
   
    private static int[] getData(BufferedImage image) {
        return image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
    }
    
    private static synchronized byte[] readAllBytes(File file) {
        try {
            return Files.readAllBytes(file.toPath());
        } catch(Exception e) {
            return new byte[0];
        }
    }
   
    private static BufferedImage readImage(File file) {
        try {
            byte[] array = readAllBytes(file);
            return ImageIO.read(new ByteArrayInputStream(array));
        } catch(Exception e) {
            return null;
        }
    }
    
    private static <T> List<List<T>> splitList(List<T> list, int n) {
        int chunkSize = list.size()/n + 1;
        ArrayList<List<T>> lists = new ArrayList<>();
        for(int start = 0; start < list.size(); start += chunkSize) {
            int end = start+chunkSize;
            if(end > list.size()) end = list.size();
            lists.add(list.subList(start, end));
        }
        return lists;
    }
   
    static class FileAB {
        final File file;
        final double[] ab;
        FileAB(File file, double[] ab) {
            this.file = file;
            this.ab = ab;
        }
       
        @Override
        public String toString() {
            return "(" + ab[0] + ", " + ab[1] + ", " + ab[2] + ") " + file.getName();
        }
    }
   
    static class CompRed implements Comparator<FileAB> {
        final double value;
       
        CompRed(double value) {
            this.value = value;
        }
       
        public int compare(FileAB a, FileAB b) {
            double va = Math.abs(a.ab[0]-value);
            double vb = Math.abs(b.ab[0]-value);
            if(va < vb) return -1;
            else if(va > vb) return 1;
            else return 0;
        }
    }
   
    static class CompGreen implements Comparator<FileAB> {
        final double value;
       
        CompGreen(double value) {
            this.value = value;
        }
       
        public int compare(FileAB a, FileAB b) {
            double va = Math.abs(a.ab[1]-value);
            double vb = Math.abs(b.ab[1]-value);
            if(va < vb) return -1;
            else if(va > vb) return 1;
            else return 0;
        }
    }
   
    static class CompBlue implements Comparator<FileAB> {
        final double value;
       
        CompBlue(double value) {
            this.value = value;
        }
       
        public int compare(FileAB a, FileAB b) {
            double va = Math.abs(a.ab[2]-value);
            double vb = Math.abs(b.ab[2]-value);
            if(va < vb) return -1;
            else if(va > vb) return 1;
            else return 0;
        }
    }
   
    private static List<FileAB> calcBrightness(List<File> pictures) {
        ArrayList<FileAB> result = new ArrayList<>();
        for(File file: pictures) {
            double[] b = null;
            try {
                BufferedImage image = readImage(file);
                int[] data = getData(image);
                b = averageBrightness(data);
            } catch(Exception e) {
            }
            if(b != null) {
                result.add(new FileAB(file, b));
            }
        }
        return result;
    }
}
 

Zurück
Oben