jisuanji1996 发表于 2014-8-27 18:49:55

我的程序可以打开E9 ov摄像头。但是无法实现录制视频.


我的程序可以打开E9 ov摄像头。但是无法实现录制视频,录制得到的文件大小为0B,下面是源代码,还请大家伙帮忙看一下是哪里有问题,万分感谢!

package com.paad.videocamera;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class VideoCameraActivity extends Activity implements SurfaceHolder.Callback {

private static final String TAG = "CameraActivity";

private Camera mcamera;
private SurfaceHolder mholder;
private MediaRecorder mmediaRecorder;

private void prepareVideoCamera() throws IllegalStateException, IOException {
    // Create a new Media Recorder.
   mmediaRecorder = new MediaRecorder();
   
    try {
      mcamera.setPreviewDisplay(mholder);
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
   
    /**
   * Listing 15-30: Preparing to record audio and video using the Media Recorder
   */
    // Unlock the Camera to allow the Media Recorder to own it.
    mcamera.unlock();

    // Assign the Camera to the Media Recorder.
    mmediaRecorder.setCamera(mcamera);

    // Configure the input sources.
    mmediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mmediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);


   //set output format
   mmediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
   //set encoder
   mmediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
   mmediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);


   mmediaRecorder.setPreviewDisplay( mholder.getSurface());
   FilesoundFile = new File(Environment.getExternalStorageDirectory()
             .getCanonicalPath() + File.separator + "myvideorecording.mp4");
      
   // Specify the output file
   mmediaRecorder.setOutputFile( soundFile.getAbsolutePath() );
      
/*
    CamcorderProfile Profile = CamcorderProfile.get( 0, CamcorderProfile.QUALITY_LOW );
    Profile.videoFrameHeight = 720;
    Profile.videoFrameWidth = 1280;
    mmediaRecorder.setProfile(Profile);
   
    FilesoundFile = new File(Environment.getExternalStorageDirectory()
            .getCanonicalPath() + File.separator + "myvideorecording.mp4");
      
    // Specify the output file
    mmediaRecorder.setOutputFile( soundFile.getAbsolutePath() );

*/
    // Prepare to record
    mmediaRecorder.prepare();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   
    SurfaceView surface = (SurfaceView)findViewById(R.id.surfaceView);
    SurfaceHolder holder = surface.getHolder();
    holder.addCallback(this);
   
    Button record = (Button)findViewById(R.id.buttonRecord);
    Button stabilize = (Button)findViewById(R.id.buttonStabilize);
    Button scanner = (Button)findViewById(R.id.buttonScanner);
   
    record.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
      startRecording();
      }   
    });
   
    stabilize.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
      enableVideoStabilization();
      }   
    });
   
    scanner.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
      stopRecording();
      }   
    });
}

public void surfaceCreated(SurfaceHolder holder) {
    this.mholder = holder;
    mcamera = Camera.open(0);      
    Camera.Parameters parameters = mcamera.getParameters();
    parameters.setRecordingHint(true);
    mcamera.setParameters(parameters);
    try {   
      mcamera.setPreviewDisplay(holder);   
    } catch (IOException e) {   
      // TODO Auto-generated catch block   
      e.printStackTrace();   
    }
   
    try {
      prepareVideoCamera();
    } catch (IllegalStateException e) {
      Log.e(TAG, "Illegal State Exception", e);
    } catch (IOException e) {
      Log.e(TAG, "I/O Exception", e);
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    this.mholder = null;
}

public void surfaceChanged(SurfaceHolder holder, int format,
                           int width, int height) {

      //开始预览   
      mcamera.startPreview();   
}

@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onPause() {
    super.onPause();
    // Reset and release the media recorder.
    mmediaRecorder.reset();
    mmediaRecorder.release();
    mcamera.lock();
   
    // Release the camera.
    mcamera.release();
}

private void stopRecording() {
    /**
   * Listing 15-32: Stopping a video recording
   */
    mmediaRecorder.stop();
      
    // Reset and release the media recorder.
    mmediaRecorder.reset();
    mmediaRecorder.release();
    mcamera.lock();
   
    try {
      prepareVideoCamera();
    } catch (IllegalStateException e) {
      Log.e(TAG, "Illegal State Exception", e);
    } catch (IOException e) {
      Log.e(TAG, "I/O Exception", e);
    }
}

private void enableVideoStabilization() {
    /**
   * Listing 15-33: Image stabilization
   */
    Camera.Parameters parameters = mcamera.getParameters();
    if (parameters.isVideoStabilizationSupported())
      parameters.setVideoStabilization(true);
    mcamera.setParameters(parameters);
}

/**
   * Listing 15-34: Adding files to the Media Store using the Media Scanner
   */
private void mediaScan(final String filePath) {
   
    MediaScannerConnectionClient mediaScannerClient = new
      MediaScannerConnectionClient() {
   
      private MediaScannerConnection msc = null;

      {
      msc = new MediaScannerConnection(
          VideoCameraActivity.this, this);
      msc.connect();
      }

      public void onMediaScannerConnected() {
      // Optionally specify a MIME Type, or
      // have the Media Scanner imply one based
      // on the filename.
      String mimeType = null;
      msc.scanFile(filePath, mimeType);
      }

      public void onScanCompleted(String path, Uri uri) {
      msc.disconnect();
      Log.d(TAG, "File Added at: " + uri.toString());
      }
    };
}

private void startRecording() {
    try {
      mmediaRecorder.start();
    } catch (IllegalStateException e) {
      mmediaRecorder.release();
      mcamera.lock();
      Log.d(TAG, "Illegal State Exception", e);
    }
}
}

jisuanji1996 发表于 2014-8-28 11:21:25

系统自带的camera是可以打开以及录像的,但是程序太大,文件就有近100个。

jisuanji1996 发表于 2014-9-1 20:03:37

有人在吗?帮忙解答一下子呗

jisuanji1996 发表于 2014-9-3 19:01:24

参数重新设置之后 能录像了
mediarecorder = new MediaRecorder();// 创建mediarecorder对象
mCamera = getCameraInstance();
// 解锁camera
mCamera.unlock();
mediarecorder.setCamera(mCamera);

// 设置录制视频源为Camera(相机)
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
// 设置录制的视频编码h263 h264
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);   
mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
// 设置视频文件输出的路径
mediarecorder.setOutputFile("/sdcard/love.mp4");
try {
   // 准备录制
   mediarecorder.prepare();
   // 开始录制
   mediarecorder.start();
} catch (IllegalStateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

但是图像有问题啊还有人帮一下子啊   

jisuanji1996 发表于 2014-9-3 19:02:37

附件是录制的视频
页: [1]
查看完整版本: 我的程序可以打开E9 ov摄像头。但是无法实现录制视频.