Recently there was a video on the android project acquisition, the requirement is to capture an event occurs after 30 seconds of video clips. 've Never done android or multimedia development, many of the concepts and techniques are not very clear, turn various forums online for a long time, roughly ideas (MediaRecorder class does not know can not meet the above requirements), is the use of PreviewCallback Camera interface By implementing onPreviewFrame (byte [] data, Camera camera) to capture each frame of data, then save the data to a file, the problem is directly saved data can not be played, the Internet was too decodeYUV420SP method to provide default YUV format transcoding RGB format, the question is whether transcoding before or after transcoding I do not know why the file should be saved is not saved directly to the suffix ".3 gp" or ". mp4" on it?
I have been in the simulator debugging, do not know the job is not necessarily required to run on a real machine.
Daren beg each significant apparitions, Thanks!
The following is my code, please Daren treatise
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
import java.io.File;
import java.io.RandomAccessFile;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class AndroidCamera extends Activity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView = null;
private SurfaceHolder mSurfaceHolder = null;
private Camera mCamera = null;
private boolean mPreviewRunning = false;
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
getWindow () setFormat (PixelFormat.TRANSLUCENT);.
requestWindowFeature (Window.FEATURE_NO_TITLE);
getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView (R.layout.main);
mSurfaceView = (SurfaceView) findViewById (R.id.surface_camera);
mSurfaceHolder = mSurfaceView.getHolder ();
mSurfaceHolder.addCallback (this);
mSurfaceHolder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@ Override
public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {
if (mPreviewRunning) {
mCamera.stopPreview ();
}
Camera.Parameters p = mCamera.getParameters ();
p.setPreviewSize (width, height);
mCamera.setPreviewCallback (new VideoData (width, height));
mCamera.setParameters (p);
try {
mCamera.setPreviewDisplay (holder);
} Catch (Exception e) {
e.printStackTrace ();
}
mCamera.startPreview ();
mPreviewRunning = true;
}
@ Override
public void surfaceCreated (SurfaceHolder holder) {
mCamera = Camera.open ();
}
@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
Log.v ("AndroidCamera", "surfaceDestroyed");
if (mCamera! = null) {
mCamera.setPreviewCallback (null);
mCamera.stopPreview ();
mPreviewRunning = false;
mCamera.release ();
mCamera = null;
}
}
}
class VideoData implements Camera.PreviewCallback {
RandomAccessFile raf = null;
byte [] h264Buff = null;
public VideoData (int width, int height) {
Log.v ("androidCamera", "new VideoData");
h264Buff = new byte [width * height * 8];
try {
Log.v ("androidCamera", "Create File: / sdcard / camera.dat start");
File file = new File ("/ sdcard/camera.3gp");
Log.v ("androidCamera", "Create File: / sdcard / camera.dat end");
raf = new RandomAccessFile (file, "rw");
} Catch (Exception ex) {
ex.printStackTrace ();
}
}
@ Override
public void onPreviewFrame (byte [] data, Camera camera) {
if (data == null) {
return;
}
.. int previewWidth = camera.getParameters () getPreviewSize () width;
int previewHeight = camera.getParameters () getPreviewSize () height;..
byte [] rgbBuffer = new byte [previewWidth * previewHeight * 3];
decodeYUV420SP (rgbBuffer, data, previewWidth, previewHeight);
try {
raf.write (rgbBuffer, 0, data.length);
} Catch (Exception ex) {
ex.printStackTrace ();
}
}
protected void finalize () {
if (null! = raf) {
try {
raf.close ();
} Catch (Exception ex) {
ex.printStackTrace ();
}
}
try {
super.finalize ();
} Catch (Throwable e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
private void decodeYUV420SP (byte [] rgbBuf, byte [] yuv420sp, int width, int height) {
final int frameSize = width * height;
if (rgbBuf == null)
throw new NullPointerException ("buffer 'rgbBuf' is null");
if (rgbBuf.lengththrow new IllegalArgumentException ("buffer 'rgbBuf' size"
+ RgbBuf.length + "
if (yuv420sp == null)
throw new NullPointerException ("buffer 'yuv420sp' is null");
if (yuv420sp.lengththrow new IllegalArgumentException ("buffer 'yuv420sp' size" + yuv420sp.length
+ "
int i = 0, y = 0;
int uvp = 0, u = 0, v = 0;
int y1192 = 0, r = 0, g = 0, b = 0;
for (int j = 0, yp = 0; juvp = frameSize + (j >> 1) * width;
u = 0;
v = 0;
for (i = 0; iy = (0xff & ((int) yuv420sp [yp])) - 16;
if (y <0) y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp [uvp + +]) - 128;
u = (0xff & yuv420sp [uvp + +]) - 128;
}
y1192 = 1192 * y;
r = (y1192 + 1634 * v);
g = (y1192 - 833 * v - 400 * u);
b = (y1192 + 2066 * u);
if (r <0) r = 0; else if (r> 262143) r = 262143;
if (g <0) g = 0; else if (g> 262143) g = 262143;
if (b <0) b = 0; else if (b> 262143) b = 262143;
rgbBuf [yp * 3] = (byte) (r >> 10);
rgbBuf [yp * 3 + 1] = (byte) (g >> 10);
rgbBuf [yp * 3 + 2] = (byte) (b >> 10);
}
}
}
}
Reply:
Save the raw data YUV and RGB, what do high MP4, 3GP This is the encoded data
Reply:
Why then the raw data should be saved documents? How to play it?
Reply:
Absolutely no idea, and some say the idea is completely wrong, it should be realized with MediaRecorder
Reply:
With MediaRecorder realize it ok, I also just realizedDid not see your code, but if there is a local dialect, not a one of you save, set a few properties like
Reply:
/ **
* Capture video
*
* @ Param filePath
* Video saved path
* /
public void captureVideo (String filePath) {
mCamera.unlock ();
mRecorder = new MediaRecorder ();
mRecorder.setCamera (mCamera);
mRecorder.setPreviewDisplay (mHolder.getSurface ());
mRecorder.setAudioSource (MediaRecorder.AudioSource.MIC);
mRecorder.setVideoSource (MediaRecorder.VideoSource.CAMERA);
/ / MRecorder.setOutputFormat (MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFormat (MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setVideoEncoder (MediaRecorder.VideoEncoder.MPEG_4_SP);
mRecorder.setVideoSize (320, 240);
/ / MRecorder.setVideoFrameRate (15);
mRecorder.setOutputFile (filePath);
try {
mRecorder.prepare ();
} Catch (Exception e) {
}
mRecorder.start (); / / Recording is now started
}
/ **
* Stop capturing video
* /
public void stopVideo () {
if (mRecorder == null)
return;
mRecorder.stop ();
mRecorder.reset (); / / You can reuse the object by going back to
/ / SetAudioSource () step
mRecorder.release (); / / Now the object cannot be reused
mRecorder = null;
try {
mCamera.reconnect ();
} Catch (Exception e) {
}
}
Also need to preview function can be achieved using SurfaceView, a lot of relevant information online
Reply:
Thank you upstairs to answer, upstairs code I have seen, have not been able to in the simulator debugging success has been reported prepare failed, do not know if the emulator does not support the cause, or where the settings are not right, and I The reason why I wanted to capture the video frame by frame, because we wanted to capture an event occurs after 30 seconds of video, two ideas, one is running after the program has saved the video, but only 60 seconds to save the length of the video, another The idea is to save the species before and after each 30-second video of the event at the time of the occurrence of a certain time, but the latter method, I do not know how to save the first 30 seconds.
Reply:
Simulator should not support
Obtained with the simulator video file is empty
As for your needs, so do not be achieved.
Your first thought hard to achieve, the second idea did not get saved by Jung constantly updated data to the memory of the way, or be able to achieve, you can search about video conversation (video conference) a class of functions.
Reply:
My first idea is to use a fixed length, similar to the stored data queue BUFFER each frame, last data in the case of filling the tail, over the team, the team data of the first team. So just make sure to capture video frames per second, you can set the length of the team. Then when an event occurs, let the program automatically stops after 30 seconds to run on it.
The problem is, the captured video data to this format conversion, but also can be saved as a video file to see where it is no concept.
In other forums say that I completely wrong idea, I do not know where the problem is, this piece does not have any multimedia experience.
Reply:
If it is not, I do not know YUV data capture directly on your PC can be converted into a file that can be played
Reply:
Will the final conclusions obtained and feasibility of the program or cause it? Find wing
Reply:
Still no
Reply:
MediaRecorder class not used
yellshine with MediaRecorder achieve that is to set up MPEG_4_SP property, so that the output data is encoded data
mpeg4Look at your program, your program gets data is RGB format, and MediaRecorder you may use the data format is not the same as playing
As long as the data was collected and MediaRecorder can play consistent data, should be able to play out
Data you can put on the camera output to RGB format and the way to play with Bitmap
Reply:
camera is absolutely necessary relevant tests on a real machine.
camera acquisition formats: JPEG, YV12, NV16, NV21, RGB_565
File file = new File (Environment.getExternalStorageDirectory (), "myvideo.yuv");
outStream = new FileOutputStream (file);
class YourStream implements Camera.PreviewCallback {
......
byte [] pdata = new byte [data.length];
int value = MetroEncoderJNI.VideoDecode (data, pdata);
outStream.write (pdata, 0, value);
......
}
Reply:
Video of it, the format is limited only by jni call ffmpeg
Reply:
Leave a mark
Learn about.
Reply:
Halo, as well as GPS-related projects, socket-related, related to the acceleration sensor, seemingly had a real machine debugging ah, the boss has not approved funding for it!
Reply:
I have tried both, the feeling is PreviewCallback the interface more suitable for use in real-time transmission, because he acquired data is a frame by frame, but it will be a little problem, the use of those soft-coding algorithm in coding efficiency when it is online too low, and the direct transfer YUV is the bandwidth too much.
MediaRecorder more suitable for use recording and collection, although I do not know is soft or hard coded, but still relatively easy to set up. But if it is for real, then a little problem, because you did not get instant frame data, you can get just a long string stream.
Reply:
Upstairs Brother, YUV bandwidth is too much of a problem you say, is the country's 3G network do? If you can use an algorithm to compress each frame YUV data is small it enough?
If MediaRecorder, then feel the problem lies in how to intercept the data.
Reply:
I have not tried this myself, but the use of the Internet was too h264 compression algorithm, the efficiency is only 1-2 frames, it is estimated, and cpu related. MediaRecorder built h264 feel like hurry up, but I'm not sure he is a hardware or software coding. Interception of data is indeed a problem, but I still did not expect a good synchronization method
Reply:
Still no clue, to the sub
Reply:
Wow, a lot of strongman, experts everywhere, little brother just thought of a real-time video capabilities within the LAN, struggling how to get frame data. . .
Read this post, help
Reply:
Can the landlord put your original code with MediaRecorder combine with, so you can record video while processing the data side?
Reply:
mark! landlord realized yet?
Reply:
Simulator can not be achieved only with a real machine. .
Reply:
MetroEncoderJNI.VideoDecode is how come?
Reply:
Hello, you said this mediarecorder I tried a lot of demo did not realize, is always the exception when setAudioSource, Do you have any record video using mediarecorder small example? And I feel this record a video adaptation of the machine is also relevant, thank you
Reply:
Embarrassed code a long time ago, it is estimated can not find it. These codes are all seen before on the Internet, behind himself did not how to study in depth, it may not help you
Reply:
I ask, after setting the encoding format MPEG_4_SP recording into 3gp video format or why it?
Reply:
In theory does not work, I am also considering, Q: 1049568282. Exchange
No comments:
Post a Comment