barzos
barzos

Reputation: 857

How to convert bitmap image to binary image in android?

I need to convert bitmap to binary image for my hw.Do u know anything about that?

Upvotes: 6

Views: 11870

Answers (5)

ik024
ik024

Reputation: 3596

Hope this helps...

Bitmap bitmapObtained =//get your bitmap
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapObtained.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

Upvotes: 0

Matthias Odisio
Matthias Odisio

Reputation: 2038

Are you looking for an algorithm to perform the conversion?

The easiest way is to compare each pixel value with a fixed threshold: if the pixel value is less than the threshold, the corresponding output pixel is black (0), else it is white (1).

If you wish to determine the threshold automatically, you may want to implement Otsu's method. That method does a correct job overall when you can't make too many assumptions about the pixels distribution in your image.

http://en.wikipedia.org/wiki/Otsu%27s_Method

As a reference, that's how it looks like in Mathematica: Binarize[image, threshold], and Binarize[img] for Otsu's method.

enter image description here

Upvotes: 3

Maidul
Maidul

Reputation: 1287

perhaps this is your code

imageID = cursor.getString(columnIndex);
              //  uri = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, "" + imageID);
                Log.v("dklfdlk",imageID);
                bitmap = BitmapFactory.decodeFile(imageID);
if (bitmap != null) {
                    newBitmap = Bitmap.createScaledBitmap(bitmap, 78, 78, true);
                    bitmap.recycle();
                    if (newBitmap != null) {
                        publishProgress(new LoadedImage(newBitmap));
}

try this

Upvotes: -1

pengwang
pengwang

Reputation: 19956

you can look this link converting Java bitmap to byte array, it can convert bitmap to binary, can then you should look at display image from byteArray

Upvotes: 0

vivek
vivek

Reputation: 284

You can use the bitmap convert function and write it to an output stream and then use the output stream to get for yourself the byte array I hope that helps

Upvotes: 0

Related Questions