bytedeco/javacv

Color patches after cropping video

Open

#983 opened on May 7, 2018

View on GitHub
 (5 comments) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Android Version - v5 (lollipop API 21) Device - Samsung Galaxy J2

Issue Cropping video while recording using camera2 API outputs color patched video in Samsung Galaxy J2.

I tested in other models (Moto g3 (API 23), Micromax canvas (API 25), Samsung Galaxy A5 (API level 24) and its working correctly.

Gradle dependencies:

compile(group: 'org.bytedeco', name: 'javacv-platform', version: '1.4.1') {
        exclude group: 'org.bytedeco.javacpp-presets'
    }
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: 3.4.1-1.4
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: 3.4.1-1.4, classifier: 'android-arm'

ImageReader format: ImageFormat.YUV_420_888 FFmpegFrameFilter format: avutil.AV_PIX_FMT_NV21

Below is the code I am using to process/convert frames using imageReader from YUV_420_888 to NV21 format

public static byte[] imageToByteArray(Image image) {
        byte[] data = null;
        if (image.getFormat() == ImageFormat.JPEG) {
            Image.Plane[] planes = image.getPlanes();
            ByteBuffer buffer = planes[0].getBuffer();
            data = new byte[buffer.capacity()];
            buffer.get(data);
            return data;
        } else if (image.getFormat() == ImageFormat.YUV_420_888) {
            data = YUV_420_888toNV21(image);

                    //NV21toJPEG(YUV_420_888toNV21(image),image.getWidth(), image.getHeight());
        }
        return data;
    }

    private static byte[] YUV_420_888toNV21(Image image) {
        byte[] nv21;
        ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
        ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
        ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();

        int ySize = yBuffer.remaining();
        int uSize = uBuffer.remaining();
        int vSize = vBuffer.remaining();

        nv21 = new byte[ySize + uSize + vSize];

        //U and V are swapped
        yBuffer.get(nv21, 0, ySize);
        vBuffer.get(nv21, ySize, vSize);
        uBuffer.get(nv21, ySize + vSize, uSize);

        return nv21;
    }

img_20180507_151620

Contributor guide