Convert a Bitmap to GrayScale in Android
Isn't that exactly what the code you're linking to does? It takes a color bitmap ("bmp"), creates a duplicate bitmap ("bm"), and then draws the color bitmap into "bm" using the filter to turn it into grayscale. From that point on, you can use "bm" as an actual grayscale bitmap and do whatever you want to do with it.
You'd need to tweak the sample a bit (it's using hard-coded sizes, you may want to just clone the size of the original bitmap), but other than that, this seems to be as ready-to-use as it gets, depending on what you want.
Converting simple image to greyscale
I found two ways while I was trying to achieve the same.
Using
ColorMatrix
private Bitmap androidGrayScale(final Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}Using
OpenCV
Download OpenCV
Library And Import as a Library Project. Add this library to your project as a reference library.
Download Links : OpenCV
private Bitmap openCVGrayScale(final Bitmap bmpOriginal, final String filePath) {
Mat imgToProcess;
Mat imgToDest = new Mat();
imgToProcess = Highgui.imread(filePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
org.opencv.android.Utils.bitmapToMat(bmpOriginal, imgToProcess);
Imgproc.cvtColor(imgToProcess, imgToDest, Imgproc.COLOR_BGR2GRAY);
Bitmap bmpGrayscale = Bitmap.createBitmap(imgToDest.cols(), imgToDest.rows(), Bitmap.Config.ARGB_8888);
org.opencv.android.Utils.matToBitmap(imgToDest, bmpGrayscale);
return bmpGrayscale;
}
Do not forget to check in your Activity
static {
if (!OpenCVLoader.initDebug()) {
android.util.Log.e("TAG", "Error");
}
}
Thanks.
Convert bitmap to Grayscale
In Android O they have introduced new Bitmap.Config
the Bitmap.Config.HARDWARE
which is an actual optimization when it comes to draw the Bitmap
to the screen. So since you are using Software rendering that is why you get the error.
Try this:
fun getGrayScaleBitmap(original: Bitmap): Bitmap {
// You have to make the Bitmap mutable when changing the config because there will be a crash
// That only mutable Bitmap's should be allowed to change config.
val bmp = original.copy(Bitmap.Config.ARGB_8888, true)
val bmpGrayscale = Bitmap.createBitmap(bmp.width, bmp.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bmpGrayscale)
val paint = Paint()
val colorMatrix = ColorMatrix()
colorMatrix.setSaturation(0f)
val colorMatrixFilter = ColorMatrixColorFilter(colorMatrix)
paint.colorFilter = colorMatrixFilter
canvas.drawBitmap(bmp, 0F, 0F, paint)
return bmpGrayscale
}
This will replace the Bitmap.Config
from Bitmap.Config.HARDWARE
to Bitmap.Config.ARGB_8888
.
Hope it helps!!!
How to apply, converting image from colored to grayscale algorithm to Android?
You need to strip off the bits that aren't part of the component you're getting, especially if there's any sign extension going on in the shifts.
int R = (q[i] >> 16) & 0xff ; //bitwise shifting
int G = (q[i] >> 8) & 0xff ;
int B = q[i] & 0xff ;
How to grayscale an image on imageview?
Hi you can make the image black n white using contrast.
See the code..
public static Bitmap createContrast(Bitmap src, double value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// get contrast value
double contrast = Math.pow((100 + value) / 100, 2);
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
// apply filter contrast for every channel R, G, B
R = Color.red(pixel);
R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(R < 0) { R = 0; }
else if(R > 255) { R = 255; }
G = Color.red(pixel);
G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(G < 0) { G = 0; }
else if(G > 255) { G = 255; }
B = Color.red(pixel);
B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(B < 0) { B = 0; }
else if(B > 255) { B = 255; }
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
Set the double value to 50 on mathod call. For Example
createContrast(Bitmap src, 50)
For more information on formulae please refer this
Related Topics
Rotating Image. Animation List or Animated Rotate? (Android)
Set Visibility of Progress Bar Gone on Completion of Image Loading Using Glide Library
What Are Intent-Filters in Android
Android - Open Target _Blank Links in Webview with External Browser
Google Map for Android My Location Custom Button
How to Disable "Next" Button on a Edittext Software Keyboard (Replace with "Done" Button)
Set Textcursordrawable Programmatically
How to Keep a Button as Pressed After Clicking on It
How to Clear The Android Stack of Activities
How to Save Data from Camera to Disk Using Mediastore on Android
Generating iOS and Android Icons in Cordova/Phonegap
Should I Use Getapplicationcontext or Activity.This in a Long Running Asynctask
Android Listview with Checkbox Problem
Getting The Current Position of a Viewpager
Disable Webview Touch Events in Android
Difference Between Getview & Getdropdownview in Spinneradapter