Android Studio: Colored image to grayscale to binary

-1

Hello I am having trouble getting my app to run. I am not sure what part of it is causing the app to fail. There isn't any message that displays, but the app will not load when I run it on the android emulator. I am trying to take an image called coloredlog and convert that from grayscale into a binary image.

public class MainActivity extends Activity {

    ImageView img;
    Button btn;

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

    //convert imageview to bitmap
    img =(ImageView) findViewById(R.id.imageView1);
    BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
    final Bitmap imgbitmap = drawable.getBitmap();


    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //convert bitmap to grayscale
            Bitmap imgnew;
            imgnew = toGrayscale(imgbitmap);
            //convert to binary
            imgnew = toBinary(imgnew);

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

}

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

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

        }
    }
    return bmpBinary;
}


}

Here is my xml code:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="210dp"
    android:contentDescription="TODO"
    app:srcCompat="@drawable/coloredlog" />
<ImageView android:layout_width="fill_parent"
    android:layout_height="210dp" android:id="@+id/imageView2" />

</LinearLayout>
java
android
binary
grayscale
asked on Stack Overflow Feb 22, 2019 by MMM • edited Feb 22, 2019 by Andrew Thompson

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0