Reputation: 857
I need to convert bitmap to binary image for my hw.Do u know anything about that?
Upvotes: 6
Views: 11870
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
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.
Upvotes: 3
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
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
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