public static ByteBuffer getScaledMatrix(Bitmap bitmap, int[] ddims) { ByteBuffer imgData = ByteBuffer.allocateDirect(ddims[0] * ddims[1] * ddims[2] * ddims[3] * 4); //ddims=[1, 3, 224, 224] imgData.order(ByteOrder.nativeOrder()); // get image pixel int[] pixels = new int[ddims[2] * ddims[3]]; Bitmap bm = Bitmap.createScaledBitmap(bitmap, ddims[2], ddims[3], false); bm.getPixels(pixels, 0, bm.getWidth(), 0, 0, ddims[2], ddims[3]); int pixel = 0; for (int i = 0; i < ddims[2]; ++i) { for (int j = 0; j < ddims[3]; ++j) { final int val = pixels[pixel++]; imgData.putFloat(((((val >> 16) & 0xFF) - 128f) / 128f)); imgData.putFloat(((((val >> 8) & 0xFF) - 128f) / 128f)); imgData.putFloat((((val & 0xFF) - 128f) / 128f)); } } if (bm.isRecycled()) { bm.recycle(); } return imgData; }
时间: 2023-03-01 10:58:51 浏览: 222
java api之ByteBuffer基础、应用场景、实战讲解
& 0xFF) - IMAGE_MEAN) / IMAGE_STD)); imgData.putFloat(((((val >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD)); imgData.putFloat((((val) & 0xFF) - IMAGE_MEAN) / IMAGE_STD); pixel++; } } return imgData; }这段代码的目的是将位图图像转换为ByteBuffer,以便进行图像处理。
阅读全文