基于没有任何转换的矩形裁剪YUV byte []

时间:2018-02-09 14:57:07

标签: android image-processing transformation codec yuv

我试图使用此SO的逻辑和图形表示。我虽然对图像感到困惑,因为其中一个跟随4:1:1,而后者则为YUV图像(NV21)执行4:2:2命名。

现在的问题是我得到了一个全部使用YUV组件的图像(转换为Bitmap / PNG),本质上是一个不可用的图像。 有什么建议来解决这个问题吗?

    private byte[] cropImage(byte[] data, Rect cropRect) {
        int dataHeight = 480;
        int dataWidth = 640;

        int totalWH = dataWidth * dataHeight;

        // make rect points even, currently the width & height is even number
        // adjust x coordinates to make them
        if (cropRect.left % 2 != 0 || cropRect.right % 2 != 0) {
            cropRect.left -= 1;
            cropRect.right -= 1;
        }
        // adjust y coordinates to make them even
        if (cropRect.top % 2 != 0 || cropRect.bottom % 2 != 0) {
            cropRect.top -= 1;
            cropRect.bottom -= 1;
        }

        int area = cropRect.width() * cropRect.height() * 3/2;
        Logger.getLogger().d("Size of byte array " + data.length + " Size of alloc area " + area);

        byte[] pixels = new byte[area];//the size of the array is the dimensions of the sub-photo

//        size.total = size.width * size.height;
//        y = yuv[position.y * size.width + position.x];
//        u = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total];
//        v = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total + (size.total / 4)];

        try {
            // copy Y plane first
            int srcOffset = cropRect.top * dataWidth;
            int destOffset = 0;
            int lengthToCopy = cropRect.width();

            int y = 0;
            for (; y < cropRect.height(); y++, srcOffset += dataWidth, destOffset += cropRect.width()) {
//            Logger.getLogger().d("IO " + srcOffset + cropRect.left + " oO " + destOffset + " LTC " + lengthToCopy);
                System.arraycopy(data, srcOffset + cropRect.left, pixels, destOffset, lengthToCopy);
            }

            Logger.getLogger().d("Completed Y copy");
            // U and V components are not-interleaved, hence their size is just 1/4th the original size
            // copy U plane
            int nonYPlanerHeight = dataHeight / 4;
            int nonYPlanerWidth = dataWidth / 4;
            srcOffset = totalWH + (cropRect.top / 4 * nonYPlanerWidth);

            for (y = 0; y < cropRect.height();
                 y++, srcOffset += nonYPlanerWidth, destOffset += cropRect.width() / 4) {
                System.arraycopy(data, srcOffset + cropRect.left / 4, pixels, destOffset, cropRect.width() / 4);
            }
            Logger.getLogger().d("Completed U copy " + y + " destOffset=" + destOffset);

            // copy V plane
            srcOffset = totalWH + totalWH / 4 + (cropRect.top / 4 * nonYPlanerWidth);

            for (y = 0; y < cropRect.height();
                 y++, srcOffset += nonYPlanerWidth, destOffset += cropRect.width() / 4) {
                System.arraycopy(data, srcOffset + cropRect.left / 4, pixels, destOffset, cropRect.width() / 4);
            }
            Logger.getLogger().d("Completed V copy " + y + " destOffset=" + destOffset);
        } catch (ArrayIndexOutOfBoundsException ae) {
            // do nothing
            Logger.getLogger().e("Exception " + ae.getLocalizedMessage());
        }

        return pixels;
    }

0 个答案:

没有答案