0

我想从 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)
4

2 回答 2

0

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

  1. 尝试在 Android 上找到 ImageIO 的包装类。
  2. 为自己编写一个类,就像 ImageIO 一样,它完全符合您的要求。
于 2013-12-04T04:00:23.160 回答
0

这一行:

java.lang.NoClassDefFoundError: javax.imageio.ImageIO

表明您没有将所有正确的代码库链接到您的项目。您需要确保此类可用。

于 2013-01-25T13:08:58.080 回答