代码如下所示:
import java.io.IOException;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
public class MainActivity extends Activity {
private Button takepicture;
private SurfaceView surfaceView=null;
private SurfaceHolder holder;
private Camera camera;
private SurfaceHolder.Callback takeback=new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//在这里实现将camer中的内容显示到这里面来
camera.open();//打开手机的摄像头
try {
camera.setPreviewDisplay(surfaceView.getHolder()); //将麝香头的类容显示到holder上面去
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();//开始展示
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findid();
surfaceView=new SurfaceView(MainActivity.this);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(takeback);
setContentView(R.layout.activity_main);
}
private void findid() {
// TODO Auto-generated method stub
surfaceView=(SurfaceView) findViewById(R.id.surfaceCammer);
takepicture=(Button) findViewById(R.id.takepicture);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import java.io.IOException;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
public class MainActivity extends Activity {
private Button takepicture;
private SurfaceView surfaceView=null;
private SurfaceHolder holder;
private Camera camera;
private SurfaceHolder.Callback takeback=new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//在这里实现将camer中的内容显示到这里面来
camera.open();//打开手机的摄像头
try {
camera.setPreviewDisplay(surfaceView.getHolder()); //将麝香头的类容显示到holder上面去
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();//开始展示
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findid();
surfaceView=new SurfaceView(MainActivity.this);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(takeback);
setContentView(R.layout.activity_main);
}
private void findid() {
// TODO Auto-generated method stub
surfaceView=(SurfaceView) findViewById(R.id.surfaceCammer);
takepicture=(Button) findViewById(R.id.takepicture);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
解决方案:40分
把setContentView(R.layout.activity_main)放在findid()之前,否则找不到控件。
camera.open();//打开手机的摄像头这一句删除,然后在onCreate(Bundle savedInstanceState)方法中的最后加上camera=Camera.open();
camera.open();//打开手机的摄像头这一句删除,然后在onCreate(Bundle savedInstanceState)方法中的最后加上camera=Camera.open();