2013-01-25 67 views
0

我想從Android中的圖像生成直方圖。我在java中找到了一些例子,但是我沒有成功移植它。如何從android中的圖像生成直方圖圖

以下是我使用的代碼 - 目前我在javax.imageio.ImageIo類上獲得NoClassDefFoundException

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) 

回答

0

這條線:

java.lang.NoClassDefFoundError: javax.imageio.ImageIO 

說明,請不要都鏈接到你的項目適當的代碼庫。你需要確保這個類是可用的。

+0

我已經鏈接了所有需要的庫。我可以在JRE系統庫上看到我附帶的項目庫。 – Alex

+0

我會仔細檢查 - 異常指示不同。 – Booger

0

javax.imageio.ImageIO是JRE中的一個類,它不存在於Dalvik機器中。所以你的代碼將運行在傳統的Java機器上(JRE,我的意思是),而不是在Android上運行。 解決方案:

  1. 嘗試在Android上查找ImageIO的包裝類。
  2. 爲你自​​己寫一個類,就像ImageIO一樣,它正是你想要的。