I changed the onPreviewFrame method of this open source Android Touch-To-Record library to transfer and resize the captured frame.
I defined "yuvIplImage" as the next in my setCameraParams () method.
IplImage yuvIplImage = IplImage.create(mPreviewSize.height, mPreviewSize.width, opencv_core.IPL_DEPTH_8U, 2);
This is my onPreviewFrame () method:
@Override public void onPreviewFrame(byte[] data, Camera camera) { long frameTimeStamp = 0L; if(FragmentCamera.mAudioTimestamp == 0L && FragmentCamera.firstTime > 0L) { frameTimeStamp = 1000L * (System.currentTimeMillis() - FragmentCamera.firstTime); } else if(FragmentCamera.mLastAudioTimestamp == FragmentCamera.mAudioTimestamp) { frameTimeStamp = FragmentCamera.mAudioTimestamp + FragmentCamera.frameTime; } else { long l2 = (System.nanoTime() - FragmentCamera.mAudioTimeRecorded) / 1000L; frameTimeStamp = l2 + FragmentCamera.mAudioTimestamp; FragmentCamera.mLastAudioTimestamp = FragmentCamera.mAudioTimestamp; } synchronized(FragmentCamera.mVideoRecordLock) { if(FragmentCamera.recording && FragmentCamera.rec && lastSavedframe != null && lastSavedframe.getFrameBytesData() != null && yuvIplImage != null) { FragmentCamera.mVideoTimestamp += FragmentCamera.frameTime; if(lastSavedframe.getTimeStamp() > FragmentCamera.mVideoTimestamp) { FragmentCamera.mVideoTimestamp = lastSavedframe.getTimeStamp(); } try { yuvIplImage.getByteBuffer().put(lastSavedframe.getFrameBytesData()); IplImage bgrImage = IplImage.create(mPreviewSize.width, mPreviewSize.height, opencv_core.IPL_DEPTH_8U, 4);
This code uses the "YUV_NV21_TO_BGR" method, which I found from this link
This method is mainly used for resolution, which I call "The problem with the green devil on Android." You can see how other Android developers are facing the same problem in other SO threads. Before adding the "YUV_NV21_TO_BGR" method, when I just transposed the transpose of YuvIplImage, more importantly, the combination of transposition, flip (with or without resizing), the resulting video had a greenish result. This method "YUV_NV21_TO_BGR" saved the day. Thanks to @David Han from the google groups stream.
You should also be aware that all this processing (transposing, flipping and resizing) in onPreviewFrame takes a lot of time, which leads to a very serious hit on your frame rate per second (FPS). When I used this code, inside the onPreviewFrame method, the resulting FPS of the recorded video was up to 3 frames / sec from 30 frames per second.
I would advise against using this approach. Rather, you can start processing after recording (transpose, flip, and resize) your video using JavaCV in AsyncTask. Hope this helps.
Sheraz nadeem
source share