java.lang.IllegalArgumentException:x必须是< bitmap.width()

时间:2016-01-29 11:02:49

标签: java android eclipse android-intent bitmap

我正在使用android中的像素RBG值来比较相似的图像。但是我在“pixel_1_final”变量上遇到了崩溃。我;图像的最后一个像素。所以如何获取图像的最后一个像素。如何解决此问题。

01-29 16:23:58.796: E/AndroidRuntime(6211): FATAL EXCEPTION: main
01-29 16:23:58.796: E/AndroidRuntime(6211): Process: com.remo.imagecomparation, PID: 6211
01-29 16:23:58.796: E/AndroidRuntime(6211): java.lang.IllegalArgumentException: x must be < bitmap.width()
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1387)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.graphics.Bitmap.getPixel(Bitmap.java:1339)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at com.remo.imagecomparation.MainActivity$3.onClick(MainActivity.java:109)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.view.View.performClick(View.java:4761)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.view.View$PerformClick.run(View.java:19767)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.os.Handler.handleCallback(Handler.java:739)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.os.Looper.loop(Looper.java:135)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at android.app.ActivityThread.main(ActivityThread.java:5312)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at java.lang.reflect.Method.invoke(Native Method)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at java.lang.reflect.Method.invoke(Method.java:372)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
01-29 16:23:58.796: E/AndroidRuntime(6211):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

代码

 identical_images = (Button)findViewById(R.id.identical_image);
        identical_images.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) 
            {
//              Log.e("compare pixels-------", "-------width------- :"+bmpimg1.getWidth());
//              Log.e("compare pixels-------", "-------Heigth------- :"+bmpimg1.getHeight());

                image_1_params.setMiddlewidth((int)((bmpimg1.getWidth())/ 2));
                image_1_params.setMiddle_width_low((int)(image_1_params.getMiddlewidth() / 2));
                image_1_params.setMiddle_width_high(image_1_params.getMiddlewidth() + image_1_params.middle_width_low);
                image_1_params.setFinalwidth(bmpimg1.getWidth());

//              Log.e("compare pixels-------width points", "["+initialwidth+","+middle_width_low+","+middlewidth+","+middle_width_high+","+finalwidth+"]");

                image_1_params.setMiddleheight((int)((bmpimg1.getHeight())/ 2));
                image_1_params.setMiddle_height_low((int)(image_1_params.getMiddleheight()/2));
                image_1_params.setMiddle_height_high(image_1_params.getMiddleheight() + image_1_params.getMiddle_height_low());
                image_1_params.setFinalheight(bmpimg1.getHeight());

//              Log.e("compare pixels-------height points", "["+initialheight+","+middle_height_low+","+middleheight+","+middle_height_high+","+finalheight+"]");

//              Log.e("compare pixels-------", "-------final width------- :"+bmpimg1.getPixel(bmpimg1.getWidth(),bmpimg1.getHeight()));

                int pixel_1_00 = bmpimg1.getPixel(image_1_params.initialwidth, image_1_params.initialheight);
//              GlobalVarsAndFunctions.RBG(pixel_1_00);

                int pixel_1_middle_low = bmpimg1.getPixel(image_1_params.getMiddle_width_low(), image_1_params.getMiddle_height_low());
//              GlobalVarsAndFunctions.RBG(pixel_1_middle_low);

                int pixel_1_middle = bmpimg1.getPixel(image_1_params.getMiddlewidth(), image_1_params.getMiddleheight());
//              GlobalVarsAndFunctions.RBG(pixel_1_middle);

                int pixel_1_middle_high = bmpimg1.getPixel(image_1_params.getMiddle_width_high(), image_1_params.getMiddle_height_high());
//              GlobalVarsAndFunctions.RBG(pixel_1_middle_high);

                int pixel_1_final = bmpimg1.getPixel(image_1_params.getFinalwidth(), image_1_params.getFinalheight());


        });

1 个答案:

答案 0 :(得分:0)

我不确定您正在使用哪些库,但是在使用getFinalWidth()搜索了一些库之后。可能是您只需要将-1附加到finalWidth()。

或者,由于getWidth()在某些在线库中不等于getFinalWidth(),您可以尝试以下内容。

int imageWidth = Math.min(image_1_params.getWidth(), image_1_params.getFinalWidth());
int imageHeight = Math.min(image_1_params.getHeight(), image_1_params.getFinalHeight());
int pixel_1_final = bmpimg1.getPixel(imageWidth, imageHeight);