我正在致力于创建一个使用 TensorFlow Lite 模型的图像超分辨率应用程序。该模型以 ByteBuffer 的形式给出输出图像,我将 ByteBuffer 转换为位图。接下来,我显示此位图,但没有显示任何内容。我正在使用的代码如下所示:
ByteBuffer out = ByteBuffer.allocate(4 * 384 * 384 * 3);
tflite.run(byteBuffer,out);
byte[] imageBytes= new byte[out.remaining()];
out.get(imageBytes);
final Bitmap outPut_Image = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
//Toast.makeText(this,tflite.toString(),Toast.LENGTH_LONG).show();
Toast.makeText(this, "Working",Toast.LENGTH_LONG).show();
ImageView imageView = (ImageView) this.findViewById(R.id.imageView2);
imageView.setImageBitmap(outPut_Image);
请告诉我我在这里做错了什么。
最佳答案
BitmapFactory.decodeByteArray() 用于压缩图像数据,例如 JPEG。
对于原始RGB888数据,我认为你应该先将其转换为ARGB_8888格式。 之后,您可以使用以下代码 fragment 创建 Bitmap 对象。
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
关于java - 如何将 ByteBuffer 转换为 Bitmap Image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248076/