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

时间:2018-10-24 07:06:54

标签: java android tensorflow tensorflow-lite

我已经建立了一个 TFLITE 模型,并且正在使用 Google的ML KIT 从我的移动应用程序访问它。尝试将我的数据转换为HashSet<Character> hs = new HashSet<Character(); hs.add('s'); hs.add('t'); hs.add('a'); hs.add('c'); int foundIndex = -1; for(int i=0; i<hs.size(); i++){ if(hs.get(i).equals('a')){ foundIndex = i; break; } } if(foundIndex == -1){ //The value wasn't in the HashSet }else{ int a = foundIndex; //The value was found } 格式以输入到分类器时遇到麻烦。

我试图将byte[1][299][299][3]设置为这种格式,但我不知道它是否在喂bytestreamleft-right,R-G-B等。

有人可以指出我可以阅读的有关解析top-bottom文件的一些文档吗?

1 个答案:

答案 0 :(得分:0)

您可以使用BitmapBitmapFactory将.png文件转换为Android InputStream

ByteArrayOutputStream stream = new ByteArrayOutputStream();

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

使用位图,您还可以获取指定位置的像素颜色。不幸的是,颜色返回为int,您必须将其分解为RGBA值。为此,请参见:https://developer.android.com/reference/android/graphics/Color

相关问题