How to convert bitmap to binary in android?

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

+4
source share
5 answers

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 value, the corresponding output pixel is black (0), otherwise it is white (1).

If you want to automatically determine the threshold, you can implement the Otsu method. This method does the job as a whole when you cannot make too many assumptions about the distribution of pixels in your image.

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

As a reference, what it looks like in Mathematica: Binarize[image, threshold] and Binarize[img] for the Otsu method.

enter image description here

+3
source

You can use the bitmap transform function and write it to the output stream, and then use the output stream to get an array of bytes for yourself Hope this helps

0
source

you can look at this link converting a Java bitmap to a byte array , it can convert a bitmap to a binary file, then you can look at displaying the image from byteArray

0
source

Hope this helps ...

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

maybe 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 it

-1
source

Source: https://habr.com/ru/post/1348190/


All Articles