English 中文(简体)
如何启用网络摄像头 在Android模拟器4.0?
原标题:How to enable webcam in Android emulator 4.0?

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

       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:
" +
                     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
        时 时
    时 时
时 时

时 时

In above code, i am getting video screen in emulator, but its like recording as squares of boxes, which is black and white boxes...!!!!!!!!!!!

在模拟器中,我无法获得现场视频, 也无法启用网络摄像头。

Interesting is, this code has successfully run in another computer,(dell webcam). In that machine its enabling web cam and recording videos very slowly, but this is recognsing and enabling camera in emulator.

另一台机器是Compaq,

请帮助激活这个摄像头设施

问题回答

旧线条, 但只是添加信息给正在处理此问题的任何人。 您只需要通过命令行启动模拟器, 替换为您想要使用相机的 Avd :

emulator -camera-front webcam0 -avd <your_avd_name>




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签