Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble getting my app to run in Android Studio. I am creating an app to convert a colored image into grayscale and

I am having trouble getting my app to run in Android Studio. I am creating an app to convert a colored image into grayscale and then to a binary image where I could return 0 or 1 depending on the binary image pixels(black or white). The conversion to grayscale is working, but the binary function will not work. Please review my code and let me know why the binary image will not display or why my binary method isn't correct. I have 3 imageviews displaying in my app - one for the colored image, one for the grayscale image, and one to display the binary image.

Here is my code:

 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.widget.ImageView; import android.graphics.Color; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //declare images ImageView originalImageView = findViewById(R.id.image); ImageView blackImageView = findViewById(R.id.blackimage); ImageView binarizedImageView = findViewById(R.id.binarizedimage); //load image Bitmap driversLog = BitmapFactory.decodeResource(getResources(), R.drawable.coloredlog); originalImageView.setBackgroundDrawable(new BitmapDrawable(driversLog)); blackImageView.setBackgroundDrawable(new BitmapDrawable( convertToGrayscale(driversLog))); //load black and white image Bitmap driverLogBinary = BitmapFactory.decodeResource(getResources(), R.id.blackimage); blackImageView.setBackground(new BitmapDrawable(driverLogBinary)); binarizedImageView.setBackgroundDrawable(new BitmapDrawable(toBinary(driverLogBinary))); } private Bitmap convertToGrayscale(Bitmap originalBitmap) { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter( colorMatrix); //creates a copy of the image Bitmap blackAndWhiteBitmap = originalBitmap.copy( Bitmap.Config.ARGB_8888, true); Paint paint = new Paint(); paint.setColorFilter(colorMatrixFilter); //displays the new black and white image Canvas canvas = new Canvas(blackAndWhiteBitmap); canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint); return blackAndWhiteBitmap; } public Bitmap toBinary(Bitmap bmpOriginal) { int width, height, threshold; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); threshold = 127; //Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal); //creates a copy of the image Bitmap bmpBinary = bmpOriginal.copy( Bitmap.Config.ARGB_8888, true); 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); } } } ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter( colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorMatrixFilter); //displays the new binary image Canvas canvas = new Canvas(); canvas.drawBitmap(bmpBinary, 0, 0, paint); return bmpBinary; } }

Here is my XML code:

      

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions