Recording video on Android using JavaCV (Updated 2014 02 17)

前端 未结 3 509
难免孤独
难免孤独 2021-02-04 19:27

I\'m trying to record a video in Android using the JavaCV lib. I need to record the video in 640x360.

I have installed everything as described in README.txt file and I f

3条回答
  •  孤独总比滥情好
    2021-02-04 19:51

    Your camera, most likely, can provide 640x480 preview frames. The fix would be to clip this frame before it is recorded, like this:

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        /* get video data */
        if (yuvIplimage != null && recording) {
            ByteBuffer bb = yuvIplimage.getByteBuffer(); // resets the buffer
            final int startY = imageWidth*(imageHeight-finalImageHeight)/2;
            final int lenY = imageWidth*finalImageHeight;
            bb.put(data, startY, lenY);
            final int startVU = imageWidth*imageHeight + imageWidth*(imageHeight-finalImageHeight)/4;
            final int lenVU = imageWidth* finalImageHeight/2;
            bb.put(data, startVU, lenVU);
    
    //      Log.v(LOG_TAG, "Writing Frame");
            try {
                long t = 1000 * (System.currentTimeMillis() - startTime);
                if (t > recorder.getTimestamp()) {
                    recorder.setTimestamp(t);
                 }
                 recorder.record(yuvIplimage);
            } catch (FFmpegFrameRecorder.Exception e) {
                 Log.e(LOG_TAG, "problem with recorder():", e);
            }
        }
    }
    

    The preview frame has semi-planar YVU format: 640x480 luminance (Y) bytes, followed by 320x240 pairs of chroma (V and U) bytes. We copy to yuvIpImage first the relevant Y, and after that - relevant VU pairs. Note that it is easy and fast because the width you want is same as the native width.

    Your camera and camera view should be initialized for 640x480, and recorder - to 640x360. Note that the efficient cropping is only possible when imageWidth==finalImageWidth.

    FIX it happens so that IplImage.getByteBuffer() resets the buffer, therefore the fix is to use a temporary bb object.

    Note that you will probably want to overlay the preview with a frame that will "hide" margins that you crop this way: our manipulations only change the recorded frames, not the CameraView.

提交回复
热议问题