1

在我的应用程序中,我想使用相机选项,所以我使用下面的代码来捕捉视频。

       public class CameraDemoActivity extends Activity
      {
  private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
  private Uri fileUri;
  public static final int MEDIA_TYPE_VIDEO = 2;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Context context = this;
    PackageManager packageManager = context.getPackageManager();

    // if device support camera?
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        //yes
        Log.i("camera", "This device has camera!");
        Toast.makeText(getBaseContext(), "Camera device", Toast.LENGTH_LONG).show();

        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,2);
        intent.putExtra(MediaStore.INTENT_ACTION_VIDEO_CAMERA, true);
        intent.putExtra(MediaStore.MEDIA_SCANNER_VOLUME, 100);

        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high


        // start the Video Capture Intent
        startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);


    }else{
        //no
        Log.i("camera", "This device has no camera!");
        Toast.makeText(getBaseContext(), "No Camera device", Toast.LENGTH_LONG).show();
    }



}

private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}



/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type)
{
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_DOWNLOADS), "MyCameraApp");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            System.out.println("MyCameraApp");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if(type == MEDIA_TYPE_VIDEO) 
    {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    }
    else 
    {
        return null;
    }

    return mediaFile;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
            System.out.println("Video saved to:"+data.getData());
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "video cancel", Toast.LENGTH_LONG).show();
            // User cancelled the video capture
        } else {
            System.out.println("video capyured failed.>.1!!!!");
            // Video capture failed, advise user
        }
    }
}

}

在上面的代码中,我在模拟器中获得了视频屏幕,但它就像录制为方块一样,这是黑色和白色的盒子......!!!!!!!!!!!!

在模拟器中,我无法获取实时视频,也无法启用网络摄像头。

有趣的是,此代码已在另一台计算机(戴尔网络摄像头)上成功运行。在该机器中,它启用网络摄像头并非常缓慢地录制视频,但这是在模拟器中识别和启用摄像头。

另一台机器是 Compaq,不幸的是它无法工作并且无法识别网络摄像头。

请任何帮助以激活此网络摄像头设施。

4

2 回答 2

1

旧线程,但只是为解决此问题的任何人添加信息。您需要做的就是通过命令行替换您要使用相机的 avd 来启动模拟器:

emulator -camera-front webcam0 -avd <your_avd_name>
于 2013-08-11T07:14:48.793 回答
1

Tom Gibara 的教程有一个更新版本。您可以将 Webcam Broadcaster 更改为使用 JMyron 而不是旧的 JMF

新的模拟器(sdk r15)管理网络摄像头;但它在集成网络摄像头方面存在一些问题(至少我的 ^^)

于 2012-05-23T10:55:04.463 回答