2016-04-24 90 views
1

我是java android studio新手。目前我正在做一个项目,即将图像从相机转换为二进制。我遵循这个链接的所有步骤(Android: Convert Grayscale to Binary Image)。然后,运行时的问题与此链接相同(Android : Converting imageview to bitmap, to grayscale, bitmap to imageview)。我解决了这个问题,但仍然无法在我的设备上运行,它显示通知不幸的是,应用程序已停止。该logcat的结果显示:Java Android Studio:将图像从相机转换为Binary

04-24 16:46:18.573 22890-22890/? I/art: Late-enabling -Xcheck:jni 
04-24 16:46:18.813 22890-22890/com.example.gabriel.image D/AndroidRuntime: Shutting down VM 
04-24 16:46:18.823 22890-22890/com.example.gabriel.image E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.gabriel.image, PID: 22890 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gabriel.image/com.example.gabriel.image.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 
at android.app.ActivityThread.access$800(ActivityThread.java:147) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5234) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference 
at com.example.gabriel.image.MainActivity.onCreate(MainActivity.java:36) 
at android.app.Activity.performCreate(Activity.java:5984) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)  
at android.app.ActivityThread.access$800(ActivityThread.java:147)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:135)  
at android.app.ActivityThread.main(ActivityThread.java:5234)  
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 

这里是我的全部编码:

public static final int REQUEST_CAPTURE = 1; 
ImageView result_photo; 
Button Binary; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button click = (Button) findViewById(R.id.BCapture); 
    result_photo = (ImageView) findViewById(R.id.imageView); 
    Binary = (Button) findViewById(R.id.btnBinary); 

    BitmapDrawable drawable = (BitmapDrawable) result_photo.getDrawable(); 
    final Bitmap result_photoBitmap = drawable.getBitmap(); 

    Binary.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //convert bitmap to grayscale 
      Bitmap result_photoNew; 
      result_photoNew = toGrayscale(result_photoBitmap); 
      //Convert to Binary 
      result_photoNew = toBinary(result_photoNew); 

      //convert bitmap to imageview 
      ImageView img_binary; 
      img_binary = (ImageView) findViewById(R.id.imageView2); 
      img_binary.setImageBitmap(result_photoNew); 
     } 
    }); 

    if (!hasCamera()) { 
     click.setEnabled(false); 
    } 


} 

public boolean hasCamera() { 
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY); 
} 

public void launchCamera(View v) { 
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(i, REQUEST_CAPTURE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_CAPTURE && resultCode == RESULT_OK) { 


     Bundle extras = data.getExtras(); 
     Bitmap photo = (Bitmap) extras.get("data"); 
     getResizedBitmap(photo, 120, 120); 
     result_photo.setImageBitmap(photo); 

    } 
} 

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

    int width = bm.getWidth(); 

    int height = bm.getHeight(); 

    float scaleWidth = ((float) newWidth)/width; 

    float scaleHeight = ((float) newHeight)/height; 

    // CREATE A MATRIX FOR THE MANIPULATION 

    Matrix matrix = new Matrix(); 

    // RESIZE THE BIT MAP 

    matrix.postScale(scaleWidth, scaleHeight); 

    // RECREATE THE NEW BITMAP 

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

    return resizedBitmap; 

} 

public Bitmap toGrayscale(Bitmap bmpOriginal){ 
    int width, height; 
    height = bmpOriginal.getHeight(); 
    width = bmpOriginal.getWidth(); 

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
    Canvas c = new Canvas(bmpGrayscale); 
    Paint paint = new Paint(); 
    ColorMatrix cm = new ColorMatrix(); 
    cm.setSaturation(0); 
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); 
    paint.setColorFilter(f); 
    c.drawBitmap(bmpOriginal, 0, 0, paint); 
    return bmpGrayscale; 
} 


public Bitmap toBinary(Bitmap bmpOriginal) { 
    int width, height, threshold; 
    height = bmpOriginal.getHeight(); 
    width = bmpOriginal.getWidth(); 
    threshold = 127; 
    //final Bitmap bmpBinary = null; 
    Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal); 

    for(int x = 0; x < width; ++x) { 
     for(int y = 0; y < height; ++y) { 
      // get one pixel color 
      int pixel = bmpOriginal.getPixel(x, y); 
      int red = Color.red(pixel); 
      int green = Color.green(pixel); 
      int blue = Color.blue(pixel); 

      //get grayscale value 
      int gray = (int)(red * 0.3 + green * 0.59 + blue *0.11); 

      //get binary value 
      if(gray < threshold){ 
       bmpBinary.setPixel(x, y, 0xFF000000); 
      } else{ 
       bmpBinary.setPixel(x, y, 0xFFFFFFFF); 
      } 

     } 
    } 
    return bmpBinary; 
} 

plz帮助我。

+0

请添加activity_main.xml代码 – USKMobility

+0

在标题或“android-studio”标签中不需要“Android Studio”,因为问题与Android Studio IDE本身无关,而与一般的Android编程无关。 –

+0

看起来像result_photo是空的,检查ID匹配您的布局XML ID。此外,您的调整大小的位图方法返回一个位图对象,但是当你调用它时,你不坐它返回的位图。 – seanAshmore

回答

0

你的问题是,这条线

BitmapDrawable drawable = (BitmapDrawable) result_photo.getDrawable(); 

被返回null,然后在这里

final Bitmap result_photoBitmap = drawable.getBitmap(); 

你将有一个NullPointerException异常。 如果非要猜你为什么越来越空,我会说,你activity_main.xmlimageView具有插入这样的提拉:

android:background="@drawable/mydrawable" 

,但它应该是这样的:

android:src="@drawable/mydrawable" 

为了得到它getDrawable()或者你将不得不打电话getBackground()