简体   繁体   English

如何将.png图像转换为字节数组-Android,TFLITE应用程序

[英]How do I convert a .png image to a Byte Array - Android, TFLITE application

I have made a TFLITE model and am using Google's ML KIT to access it from my mobile application. 我已经制作了TFLITE模型,并且正在使用Google的ML KIT从我的移动应用程序访问它。 I have run into trouble with trying to get my data into the byte[1][299][299][3] format that I need for feeding to my classifier. 尝试将我的数据转换为需要输入到分类器的byte[1][299][299][3]格式时遇到了麻烦。

I was trying to arrange a bytestream into that format but I don't know if it's feeding left-right , top-bottom , RGB, etc. 我试图将bytestream安排为这种格式,但是我不知道它是否在left-righttop-bottom ,RGB等进纸。

Can anyone point me to some documentation I can read about parsing .png files? 谁能指出我一些我可以阅读的有关解析.png文件的文档?

You can convert your .png file to an Android Bitmap using a BitmapFactory and an InputStream . 您可以使用BitmapFactoryInputStream将.png文件转换为Android Bitmap

ByteArrayOutputStream stream = new ByteArrayOutputStream();

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();

With your Bitmap, you can also get the color of a pixel at a specified position. 使用位图,您还可以获取指定位置的像素颜色。 Unfortunately, the color is returned as int , which you have to decompose to your RGBA values. 不幸的是,颜色返回为int ,您必须将其分解为RGBA值。 For that, see: https://developer.android.com/reference/android/graphics/Color 为此,请参见: https : //developer.android.com/reference/android/graphics/Color

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

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