java.lang.illegalargumentException:字节缓冲区的大小和形状不匹配

发布于 2025-01-20 19:41:32 字数 412 浏览 7 评论 0原文

        TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.UINT8);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4* imageSize * imageSize * 3);
        byteBuffer.order(ByteOrder.nativeOrder());
        inputFeature0.loadBuffer(byteBuffer);

Java.lang.IllegalArgumentException:字节缓冲区的大小和形状不匹配。 理想情况下,如何定义确切的缓冲尺寸?输入图像大小为224*224。

        TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.UINT8);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4* imageSize * imageSize * 3);
        byteBuffer.order(ByteOrder.nativeOrder());
        inputFeature0.loadBuffer(byteBuffer);

java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.
Ideally how to define the exact buffer size? The input image size is 224*224.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2025-01-27 19:41:32

有一次我遇到类似的问题,通过使用下面的代码解决了。

解决方案1

TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.UINT8);

            Bitmap input=Bitmap.createScaledBitmap(imageBitmap,224,224,true);
            TensorImage image=new TensorImage(DataType.UINT8);
            image.load(input);
            ByteBuffer byteBuffer=image.getBuffer();
            inputFeature0.loadBuffer(byteBuffer);

解决方案2

 ByteBuffer byteBuffer = bitmapToByteBuffer(imageBitmap, 224, 224)

 public ByteBuffer bitmapToByteBuffer(Bitmap image, int width, int height) {
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * width * height * 3);
            byteBuffer.order(ByteOrder.nativeOrder());
            // get 1D array of width * height pixels in image
            int[] intValues = new int[width * height];
            image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
    
            // iterate over pixels and extract R, G, and B values. Add to bytebuffer.
            int pixel = 0;
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    int val = intValues[pixel++]; // RGB
                    byteBuffer.putFloat(((val >> 16) & 0xFF) * (1.f / 255.f));
                    byteBuffer.putFloat(((val >> 8) & 0xFF) * (1.f / 255.f));
                    byteBuffer.putFloat((val & 0xFF) * (1.f / 255.f));
                }
            }
            return byteBuffer;
        }

Once I faced similar issue solved by using below code.

Solution 1

TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.UINT8);

            Bitmap input=Bitmap.createScaledBitmap(imageBitmap,224,224,true);
            TensorImage image=new TensorImage(DataType.UINT8);
            image.load(input);
            ByteBuffer byteBuffer=image.getBuffer();
            inputFeature0.loadBuffer(byteBuffer);

Solution 2

 ByteBuffer byteBuffer = bitmapToByteBuffer(imageBitmap, 224, 224)

 public ByteBuffer bitmapToByteBuffer(Bitmap image, int width, int height) {
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * width * height * 3);
            byteBuffer.order(ByteOrder.nativeOrder());
            // get 1D array of width * height pixels in image
            int[] intValues = new int[width * height];
            image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
    
            // iterate over pixels and extract R, G, and B values. Add to bytebuffer.
            int pixel = 0;
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    int val = intValues[pixel++]; // RGB
                    byteBuffer.putFloat(((val >> 16) & 0xFF) * (1.f / 255.f));
                    byteBuffer.putFloat(((val >> 8) & 0xFF) * (1.f / 255.f));
                    byteBuffer.putFloat((val & 0xFF) * (1.f / 255.f));
                }
            }
            return byteBuffer;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文