Java Android Studio:将图像从相机转换为二进制

时间:2016-04-24 08:59:16

标签: java android image-processing bitmap android-drawable

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

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;
}

请帮助我。

1 个答案:

答案 0 :(得分:0)

你的问题是这一行

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

返回null然后在这里

final Bitmap result_photoBitmap = drawable.getBitmap();

你将有一个NullPointerException。 如果我必须猜测你为什么会变空,我会说在你activity_main.xml imageView中插入了这样的drawable:

android:background="@drawable/mydrawable"

但它应该是这样的:

android:src="@drawable/mydrawable"

要使用getDrawable()获取,或者您必须拨打getBackground()