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.
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!!!
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.
Android : Converting imageview to bitmap, to grayscale, bitmap to imageview
If above is your full code then, basic problem is there, you haven't define btn
. You need to define it before using it, else when you ar egoing to click on the button it will not work. and this might closing your application.
btn=(Button) findViewById(R.id.button1);
Converting Bitmap(RGB_565) to Grayscale(8 bit) Android
for the CameraPicture this applies:
If setPreviewFormat(int) is never called, the default will be the
YCbCr_420_SP (NV21) format.
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
Bitmap bmp you can configure like:
Bitmap bmp = Bitmap.createBitmap(frameWidth, frameHeight, Bitmap.Config.ARGB_8888); //or RGB_565 if you prefer.
for accessing bmp data I only found something like this: http://upload.wikimedia.org/wikipedia/commons/c/c4/BMPfileFormat.png
hope it helps
Converting color bitmap to grayscale image using only the blue channel using a colormatrix
The correct matrix for getting a gray scaled image from the blue channel is the following:
float[] arrayForColorMatrix = new float[] {0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0};
From the Android documentation:
When applied to a color [r, g, b, a], the resulting color is computed as (after clamping):
R' = a*R + b*G + c*B + d*A + e;
G' = f*R + g*G + h*B + i*A + j;
B' = k*R + l*G + m*B + n*A + o;
A' = p*R + q*G + r*B + s*A + t;
Using my arrayForColorMatrix
I get the blue value for every value of the RGB color components, which results in a gray scaled bitmap based on the blue channel.
Related Topics
Google Speech Recognition Timeout
Android 2.2 Mediaplayer Is Working Fine with One Shoutcast Url But Not with the Other One
Android Create Folders in Internal Memory
Copy Database from Assets Folder in Unrooted Device
Android Studio - Emulator - Eglsurfaceattrib Not Implemented
How to Parse a JSON Object in Android
Coloring Buttons in Android with Material Design and Appcompat
What Are My Options for Storing Data When Using React Native? (iOS and Android)
How to Show a Dialog to Confirm That the User Wishes to Exit an Android Activity
Enable and Disable a Broadcast Receiver
Android - How to Unregister a Receiver Created in the Manifest
Common Class for Asynctask in Android
Is There a List of Screen Resolutions for All Android Based Phones and Tablets
Shell Script in Android Gives [: Not Found
Securityexception: Permission Denied (Missing Internet Permission)