如何从android中的图像生成直方图

时间:2013-01-25 13:03:54

标签: android histogram

我想从Android中的图像生成直方图。我确实在java中找到了一些例子,但我没有成功移植它。

以下是我使用的代码 - 目前我在NoClassDefFoundException课程上获得了javax.imageio.ImageIo

public static byte[] histogramEqualization(String path) {

        int red;
        int green;
        int blue;
        int alpha;
        int newPixel = 0;

        original = null;
        try {
            original = ImageIO.read(new URL(path));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Get the Lookup table for histogram equalization
        ArrayList<int[]> histLUT = histogramEqualizationLUT(original);

        BufferedImage histogramEQ = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());

        for(int i=0; i<original.getWidth(); i++) {
            for(int j=0; j<original.getHeight(); j++) {

                // Get pixels by R, G, B
                alpha = new Color(original.getRGB (i, j)).getAlpha();
                red = new Color(original.getRGB (i, j)).getRed();
                green = new Color(original.getRGB (i, j)).getGreen();
                blue = new Color(original.getRGB (i, j)).getBlue();

                // Set new pixel values using the histogram lookup table
                red = histLUT.get(0)[red];
                green = histLUT.get(1)[green];
                blue = histLUT.get(2)[blue];

                // Return back to original format
                newPixel = colorToRGB(alpha, red, green, blue);

                // Write pixels into image
                histogramEQ.setRGB(i, j, newPixel);

            }
        }

      //create a ByteArrayOutputStream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] imageInByte = null;
        //write our image to it
        try {
            ImageIO.write( histogramEQ, "png", baos );
             //flush the stream
            baos.flush();

            //get the image in byte form
            imageInByte = baos.toByteArray();

            //close the stream
            baos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




//        return histogramEQ;
        return imageInByte;

    }

    // Get the histogram equalization lookup table for separate R, G, B channels
    public static ArrayList<int[]> histogramEqualizationLUT(BufferedImage input) {

        // Get an image histogram - calculated values by R, G, B channels
        ArrayList<int[]> imageHist = imageHistogram(input);

        // Create the lookup table
        ArrayList<int[]> imageLUT = new ArrayList<int[]>();

        // Fill the lookup table
        int[] rhistogram = new int[256];
        int[] ghistogram = new int[256];
        int[] bhistogram = new int[256];

        for(int i=0; i<rhistogram.length; i++) rhistogram[i] = 0;
        for(int i=0; i<ghistogram.length; i++) ghistogram[i] = 0;
        for(int i=0; i<bhistogram.length; i++) bhistogram[i] = 0;

        long sumr = 0;
        long sumg = 0;
        long sumb = 0;

        // Calculate the scale factor
        float scale_factor = (float) (255.0 / (input.getWidth() * input.getHeight()));

        for(int i=0; i<rhistogram.length; i++) {
            sumr += imageHist.get(0)[i];
            int valr = (int) (sumr * scale_factor);
            if(valr > 255) {
                rhistogram[i] = 255;
            }
            else rhistogram[i] = valr;

            sumg += imageHist.get(1)[i];
            int valg = (int) (sumg * scale_factor);
            if(valg > 255) {
                ghistogram[i] = 255;
            }
            else ghistogram[i] = valg;

            sumb += imageHist.get(2)[i];
            int valb = (int) (sumb * scale_factor);
            if(valb > 255) {
                bhistogram[i] = 255;
            }
            else bhistogram[i] = valb;
        }

        imageLUT.add(rhistogram);
        imageLUT.add(ghistogram);
        imageLUT.add(bhistogram);

        return imageLUT;

    }

    private static int colorToRGB(int alpha, int red, int green, int blue) {

        int newPixel = 0;
        newPixel += alpha; newPixel = newPixel << 8;
        newPixel += red; newPixel = newPixel << 8;
        newPixel += green; newPixel = newPixel << 8;
        newPixel += blue;

        return newPixel;

    }

    public static ArrayList<int[]> imageHistogram(BufferedImage input) {

        int[] rhistogram = new int[256];
        int[] ghistogram = new int[256];
        int[] bhistogram = new int[256];

        for(int i=0; i<rhistogram.length; i++) rhistogram[i] = 0;
        for(int i=0; i<ghistogram.length; i++) ghistogram[i] = 0;
        for(int i=0; i<bhistogram.length; i++) bhistogram[i] = 0;

        for(int i=0; i<input.getWidth(); i++) {
            for(int j=0; j<input.getHeight(); j++) {

                int red = new Color(input.getRGB (i, j)).getRed();
                int green = new Color(input.getRGB (i, j)).getGreen();
                int blue = new Color(input.getRGB (i, j)).getBlue();

                // Increase the values of colors
                rhistogram[red]++; ghistogram[green]++; bhistogram[blue]++;

            }
        }

        ArrayList<int[]> hist = new ArrayList<int[]>();
        hist.add(rhistogram);
        hist.add(ghistogram);
        hist.add(bhistogram);

        return hist;

    }

这是我得到的错误。

01-25 18:05:37.548: E/AndroidRuntime(3514): FATAL EXCEPTION: main
01-25 18:05:37.548: E/AndroidRuntime(3514): java.lang.NoClassDefFoundError: javax.imageio.ImageIO
01-25 18:05:37.548: E/AndroidRuntime(3514):     at com.example.histogramsample.MainActivity.histogramEqualization(MainActivity.java:57)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at com.example.histogramsample.MainActivity.onCreate(MainActivity.java:33)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.Activity.performCreate(Activity.java:4465)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.os.Looper.loop(Looper.java:137)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at android.app.ActivityThread.main(ActivityThread.java:4514)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at java.lang.reflect.Method.invoke(Method.java:511)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-25 18:05:37.548: E/AndroidRuntime(3514):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

这一行:

java.lang.NoClassDefFoundError: javax.imageio.ImageIO

表示您没有链接到项目的所有正确代码库。您需要确保此课程可用。

答案 1 :(得分:0)

javax.imageio.ImageIO是来自JRE的一个类,它在Dalvik机器中不存在。所以你的代码将运行在传统的Java机器上(我的意思是JRE)不在Android上运行。 解决方案:

  1. 尝试在Android上找到ImageIO的包装类。
  2. 为自己写一个类似ImageIO的课程,它完全符合您的要求。