当我在计算机上运行我的 android 相机应用程序时,网络摄像头无法正常工作。
这里是代码。清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.examples"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="300px"
android:paddingLeft="5px"
android:paddingTop="5px"
android:paddingRight="5px">
<ImageButton
android:layout_width="50px"
android:textColor="#ffffff"
android:layout_height="50px"
android:id="@+id/cameraClick "
/>
</LinearLayout>
</RelativeLayout>
文件
package com.examples;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity implements SurfaceHolder.Callback{
private Camera camera;
private ImageButton cameraClick;
private SurfaceHolder mHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
mHolder = surfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraClick = (ImageButton) findViewById(R.id.cameraClick);
cameraClick.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
camera.takePicture(shutterCallback, rawCallback,jpegCallback);
}
});
}
// Handles when shutter open
ShutterCallback shutterCallback = new ShutterCallback()
{
public void onShutter()
{
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
// we do something using return byte[] of taken image.
}
};
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)
{
// Set camera preview size,orientation,rotation using parameters
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
当我运行此代码时,网络摄像头无法启动。请帮我解决这个问题。谢谢