【问题标题】:Plotting Image Histogram Using Androidplot使用 Androidplot 绘制图像直方图
【发布时间】:2014-05-23 23:02:20
【问题描述】:

我是一名初学者程序员,我正在开发一个处理图像并生成其直方图的 android 应用程序。我正在使用 AndroidPlot 在直方图中绘制图像的不同颜色通道,但我遇到了一些错误和异常。

这是我的代码:

public class ImgHistogram extends Activity {

ImageView ImageView;
LinearLayout ll;

ImageView imgView;
    private Bitmap source ;



private XYPlot xyPlot;
public XYSeries rSerie;

 int[] intensities={
         0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,
           101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,
           201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
 } ;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.histo);



   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

   imgView = (ImageView) findViewById(R.id.img);

    Bundle bundle = this.getIntent().getExtras();
      source=(Bitmap) bundle.getParcelable("img");
      imgView.setImageBitmap(source);

    // initialize our XYPlot reference:
        xyPlot = (XYPlot) findViewById(R.id.xyplot);



        Bitmap image = Bitmap.createBitmap(source.getWidth(),source.getHeight(), Bitmap.Config.ARGB_8888);
        ArrayList<Integer> rVals = new ArrayList<Integer>();
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {

        int color = image.getPixel(x, y);
       rVals.add((color >> 16) & 0xff); //red channel

            }}
        rSerie = new SimpleXYSeries(
                rVals, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "red channel");


        // Create a formatter to format Line and Point of income series
        LineAndPointFormatter format = new LineAndPointFormatter(
            Color.rgb(0, 0, 255),                   // line color
            Color.rgb(200, 200, 200),               // point color
            Color.rgb(10, 20, 20),              // fill color (none)
            null );                                    



     // add  series to the xyplot:
        xyPlot.addSeries(rSerie, format);


        // Formatting the Domain Values ( X-Axis )
        xyPlot.setDomainValueFormat(new Format() {

            private static final long serialVersionUID = 1L;

            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
                return new StringBuffer( intensities[ ( (Number)obj).intValue() ]  );
            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
            }
        });

        xyPlot.setDomainLabel("");
        xyPlot.setRangeLabel("");

        // Increment X-Axis by 1 value
        xyPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);

        xyPlot.getGraphWidget().setRangeLabelWidth(50);

        // Reduce the number of range labels
        xyPlot.setTicksPerRangeLabel(2);

        // Reduce the number of domain labels
        xyPlot.setTicksPerDomainLabel(2);

        // Remove all the developer guides from the chart
    //    xyPlot.disableAllMarkup();


    }

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }

}


这是堆栈跟踪:

05-23 23:52:21.731: E/AndroidRuntime(30404): FATAL EXCEPTION: main
05-23 23:52:21.731: E/AndroidRuntime(30404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamaker.magipixel/com.dreamaker.magipixel.processing.ImgHistogram}: java.lang.NullPointerException
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.os.Looper.loop(Looper.java:137)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.main(ActivityThread.java:5419)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at java.lang.reflect.Method.invoke(Method.java:525)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at dalvik.system.NativeStart.main(Native Method)
05-23 23:52:21.731: E/AndroidRuntime(30404): Caused by: java.lang.NullPointerException
05-23 23:52:21.731: E/AndroidRuntime(30404):    at com.dreamaker.magipixel.processing.ImgHistogram.onCreate(ImgHistogram.java:110)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.Activity.performCreate(Activity.java:5372)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-23 23:52:21.731: E/AndroidRuntime(30404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
05-23 23:52:21.731: E/AndroidRuntime(30404):    ... 11 more

希望有人能帮助我!

【问题讨论】:

  • 您在方法 onCreate 中的 line 110 中有一个空引用异常,但是如果没有您的代码行号,我们无法知道它到底在哪里 => 是什么原因造成的。跨度>
  • 是的,这是一个空指针异常,这是第 110 行:xyPlot.addSeries(rSerie, format);

标签: image histogram androidplot


【解决方案1】:

鉴于在有问题的行 (ImgHistogram.java:110) 中您只有 1 个可能导致异常的对象 - xyPlot

xyPlot.addSeries(rSerie, format);

这表明findViewByID 返回 null =xyPlot 为 null。对此有一个问题:findViewByID returns null accepted answer 应该可以解决您的问题:

等到onFinishInflate()

【讨论】:

  • 它没有解决问题,可能问题的根源是变量“强度”。
  • 好的,你能检查一下 xyPlot 实际上是否为空吗? - 它应该为空,在这种情况下我的解决方案应该有效,但如果没有,请确认它是否为空。
  • 它显示了一个带有 ArrayIndexOutOfBoundsException 异常的空 xyPlot
  • 它解决了这个问题,现在你有一个不同的问题 - 为这个新问题创建一个新问题,并提供新代码并标记引发新异常的行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
相关资源
最近更新 更多