繁体   English   中英

如何在Android中将位图图像转换为二进制图像?

[英]How to convert bitmap image to binary image in android?

我需要将硬件的位图转换为二进制图像,您对此一无所知吗?

您是否正在寻找执行转换的算法?

最简单的方法是将每个像素值与固定阈值进行比较:如果像素值小于阈值,则对应的输出像素为黑色(0),否则为白色(1)。

如果您希望自动确定阈值,则可能需要实施Otsu的方法。 当您不能对图像中的像素分布做出太多假设时,该方法总体上是正确的。

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

作为参考,这在Mathematica中是这样的:Otsu方法的Binarize[image, threshold]Binarize[img]

在此处输入图片说明

希望这可以帮助...

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

您可以使用位图转换功能并将其写入输出流,然后使用输出流自行获取字节数组,希望对您有所帮助

您可以查看将Java位图转换为字节数组的链接,可以将位图转换为二进制,然后可以查看byteArray的显示图像

也许这是你的代码

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));
}

尝试这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM