package com.example.preview;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements SurfaceHolder.Callback
{
Button start,stop;
Camera camera;
private final String tag = "VideoServer";
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.button1);
start.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
start_camera();
}
});
stop = (Button)findViewById(R.id.button2);
stop.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
stop_camera();
}
});
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void start_camera()
{
try
{
camera = Camera.open();
}
catch(RuntimeException e)
{
Log.e(tag, "init_camera: " + e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
//param.setPreviewFrameRate(20);
param.setPreviewSize(176,144);
camera.setParameters(param);
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}
catch (Exception e)
{
Log.e(tag, "init_camera: " + e);
return;
}
}
private void stop_camera()
{
camera.stopPreview();
camera.release();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
这是在单击开始按钮时预览的代码。 但是有一些错误。点击预览后停止弹出。任何人的任何建议都会令人感激。我添加了所需的所有权限。仍然无法正常工作
logcat错误
11-28 11:01:49.621: E/AndroidRuntime(3558): FATAL EXCEPTION: main
11-28 11:01:49.621: E/AndroidRuntime(3558): java.lang.NullPointerException
11-28 11:01:49.621: E/AndroidRuntime(3558): at com.example.preview.MainActivity.start_camera(MainActivity.java:64)
11-28 11:01:49.621: E/AndroidRuntime(3558): at com.example.preview.MainActivity.access$0(MainActivity.java:51)
11-28 11:01:49.621: E/AndroidRuntime(3558): at com.example.preview.MainActivity$1.onClick(MainActivity.java:32)
11-28 11:01:49.621: E/AndroidRuntime(3558): at android.view.View.performClick(View.java:3511)
11-28 11:01:49.621: E/AndroidRuntime(3558): at android.view.View$PerformClick.run(View.java:14109)
11-28 11:01:49.621: E/AndroidRuntime(3558): at android.os.Handler.handleCallback(Handler.java:605)
11-28 11:01:49.621: E/AndroidRuntime(3558): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 11:01:49.621: E/AndroidRuntime(3558): at android.os.Looper.loop(Looper.java:137)
11-28 11:01:49.621: E/AndroidRuntime(3558): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-28 11:01:49.621: E/AndroidRuntime(3558): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 11:01:49.621: E/AndroidRuntime(3558): at java.lang.reflect.Method.invoke(Method.java:511)
11-28 11:01:49.621: E/AndroidRuntime(3558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-28 11:01:49.621: E/AndroidRuntime(3558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-28 11:01:49.621: E/AndroidRuntime(3558): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您是否已在AndroidManifest.xml
?
//相机权限
更新:在此SO上使用Narendra Pal
的答案Camera Tutorial for Android (using surfaceview)
在没有后置摄像头Camera.open() returns null
的设备上。尝试Camera.open(0)
获取前置摄像头。如果您使用的是模拟器,请确保已为模拟器设备启用了相机。
答案 1 :(得分:0)
我在这里猜到的是,您通过调用Camera
获得的CAmera.open()
实例是null
。你在这里的catch区块中有任何异常吗?
try
{
camera = Camera.open();
}
catch(RuntimeException e)
{
Log.e(tag, "init_camera: " + e);
return;
}