public class MainActivity extends Activity implements SurfaceHolder.Callback,
PictureCallback, OnClickListener {
private SurfaceView surfaceView;
private SurfaceHolder holder;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.myCameraView);
// 设置该视图是否可以接收焦点
surfaceView.setFocusable(true);
// 设置该视图是否可以在触摸模式中接收焦点
surfaceView.setFocusableInTouchMode(true);
// 设置该视图是否可以触发点击事件
surfaceView.setClickable(true);
// 设置该视图的点击事件
surfaceView.setOnClickListener(this);
// SurfaceView中的getHolder方法可以获取到一个SurfaceHolder实例
holder = surfaceView.getHolder();
// 为了实现照片预览功能,需要将SurfaceHolder的类型设置为PUSH
// 这样,画图缓存就由Camera类来管理,画图缓存是独立于Surface的
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (checkCameraHardware(this)) {
camera = Camera.open();
} else {
Toast.makeText(this, "没有检测到可用设备", Toast.LENGTH_SHORT).show();
return;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Uri imageUri = this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
try {
OutputStream outputStream = this.getContentResolver().openOutputStream(imageUri);
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
if (camera != null) {
camera.setPreviewDisplay(holder);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
/**
* 如果你的预览可能发生改变或旋转,要在这个方法中注意这些事件。 确保在调整大小或重新布局格式的时候停止预览。
* */
// 如果预览不存在,就直接返回
if (holder.getSurface() == null) {
return;
}
// 在预览发生改变之前要停掉当前的预览
camera.stopPreview();
// 获取当前设备相机参数
Camera.Parameters parameters = camera.getParameters();
// 如果是竖屏
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
} else {
parameters.set("orientation", "landscape");
}
// 获取系统所支持的所有颜色特效,又符合条件的,则设置
// List<String> colorEffects = parameters.getSupportedColorEffects();
// for (String colorEffect : colorEffects) {
// if (colorEffect.equals(Camera.Parameters.EFFECT_SOLARIZE)) {
// parameters.setColorEffect(colorEffect);
// }
// }
// // 设置完成需要再次调用setParameter方法才能生效
// camera.setParameters(parameters);
int bestWidth = 0;
int bestHeight = 0;
List<Size> sizes = parameters.getSupportedPreviewSizes();
for (Size size : sizes) {
if (size.width > bestWidth && size.height > bestHeight
&& size.width < width && size.height < height) {
bestWidth = size.width;
bestHeight = size.height;
}
}
if (bestWidth != 0 && bestHeight != 0) {
parameters.setPreviewSize(bestWidth, bestHeight);
surfaceView
.setLayoutParams(new LayoutParams(bestWidth, bestHeight));
}
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (camera != null) {
camera.stopPreview();
}
}
// 实现SurfaceView的点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.myCameraView:
camera.takePicture(null, null, null, this);
break;
default:
break;
}
}
}
实体手机上测试,错误提示瞬间消失,没有来得及截取错误消息。
布局文件中就只有一个SurfaceView,主清单文件中也就是加入了相机权限以及写入到SD卡的权限。
菜鸟坐等回复