2016-04-24 92 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()