How To Open Cv::videowriter In Android
I'm using OpenCV 2.4.8 in Android using JNI. I opening the camera with VideoCapture and I want to record it. I have the image in a cv::Mat, it appear in the screen correctly. But,
Solution 1:
As I know OpenCV4adnroid doesn’t support video reading and writing. Try to rebuild your Opencv with an encoder option: (such: WITH_FFMPEG=YES or WITH_VFW=YES )
OR try save a sequence of images then encode a video from this sequence from java code I tried next proposition (ref)
publicvoidimageToMP4(BufferedImage bi) {
// A transform to convert RGB to YUV colorspaceRgbToYuv420transform=newRgbToYuv420(0, 0);
// A JCodec native picture that would hold source image in YUV colorspacePicturetoEncode= Picture.create(bi.getWidth(), bi.getHeight(), ColorSpace.YUV420);
// Perform conversion
transform.transform(AWTUtil.fromBufferedImage(bi), yuv);
// Create MP4 muxerMP4Muxermuxer=newMP4Muxer(sink, Brand.MP4);
// Add a video trackCompressedTrackoutTrack= muxer.addTrackForCompressed(TrackType.VIDEO, 25);
// Create H.264 encoderH264Encoderencoder=newH264Encoder(rc);
// Allocate a buffer that would hold an encoded frameByteBuffer_out= ByteBuffer.allocate(ine.getWidth() * ine.getHeight() * 6);
// Allocate storage for SPS/PPS, they need to be stored separately in a special place of MP4 file
List<ByteBuffer> spsList = newArrayList<ByteBuffer>();
List<ByteBuffer> ppsList = newArrayList<ByteBuffer>();
// Encode image into H.264 frame, the result is stored in '_out' bufferByteBufferresult= encoder.encodeFrame(_out, toEncode);
// Based on the frame above form correct MP4 packet
H264Utils.encodeMOVPacket(result, spsList, ppsList);
// Add packet to video track
outTrack.addFrame(newMP4Packet(result, 0, 25, 1, 0, true, null, 0, 0));
// Push saved SPS/PPS to a special storage in MP4
outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList));
// Write MP4 header and finalize recording
muxer.writeHeader();
}
You can download JCodec library from a project web site or via Maven, for this add the below snippet to your pom.xml:
<dependency><groupId>org.jcodec</groupId><artifactId>jcodec</artifactId><version>0.1.3</version></dependency>
Android: Android users can use something like below to convert Android Bitmap object to JCodec native format:
publicstatic Picture fromBitmap(Bitmap src) {
Picturedst= Picture.create((int)src.getWidth(), (int)src.getHeight(), RGB);
fromBitmap(src, dst);
return dst;
}
publicstaticvoidfromBitmap(Bitmap src, Picture dst) {
int[] dstData = dst.getPlaneData(0);
int[] packed = newint[src.getWidth() * src.getHeight()];
src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());
for (inti=0, srcOff = 0, dstOff = 0; i < src.getHeight(); i++) {
for (intj=0; j < src.getWidth(); j++, srcOff++, dstOff += 3) {
intrgb= packed[srcOff];
dstData[dstOff] = (rgb >> 16) & 0xff;
dstData[dstOff + 1] = (rgb >> 8) & 0xff;
dstData[dstOff + 2] = rgb & 0xff;
}
}
}
Solution 2:
('M','J','P','G') is the only supported by android while using .avi ext. Most important of all is to #include stdio.h, without this you wont be able to open VideoWriter video
Solution 3:
cv::VideoWriter writer;
writer.open("your_mp4_file_path", cv::VideoWriter::fourcc('H', '2', '6', '4'),
15, //framerate
cv::Size(720, 1280),
true);
writer << mat_frame;
// remember writer.release() when finish
android opencv 4.5.2, opencv with ffmpeg + openh264, work well for me
Post a Comment for "How To Open Cv::videowriter In Android"