在Android中调用摄像头进行拍照有两种方式,一种是使用系统的拍照Activity,另一种是使用Camera对象自己实现拍照功能
一、调用系统拍照Activity
public class Main extends Activity implements OnClickListener
{
public ImageView imageView;
private ImageView ivFocus;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
btnTakePicture.setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.imageview);
ivFocus = new ImageView(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1)
{
if (resultCode == Activity.RESULT_OK)
{
Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
// 存成320*240和50*50 如果是垂直方向,需要对cameraBitmap旋转90度
MediaStore.Images.Media.insertImage(getContentResolver(), cameraBitmap, null, null);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onClick(View view)
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/test.jpg")));
ListView a;
startActivityForResult(intent, 1);
}
}
二、使用Camera对象
public class CameraPreview extends Activity
{
private Preview preview;
private ImageView ivFocus;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
preview = new Preview(this);
setContentView(preview);
ivFocus = new ImageView(this);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
preview.takePicture();
return super.onTouchEvent(event);
}
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder holder;
private Camera camera;
// 创建一个PictureCallback对象,并实现其中的onPictureTaken方法
private PictureCallback pictureCallback = new PictureCallback()
{
// 该方法用于处理拍摄后的照片数据
public void onPictureTaken(byte[] data, Camera camera)
{
// data参数值就是照片数据,将这些数据以key-value形式保存,以便其他调用该Activity的程序可
// 以获得照片数据
getIntent().putExtra("bytes", data);
setResult(20, getIntent());
// 停止照片拍摄
camera.stopPreview();
camera = null;
// 关闭当前的Activity
finish();
}
};
// Preview类的构造方法
public Preview(Context context)
{
super(context);
// 获得SurfaceHolder对象
holder = getHolder();
// 指定用于捕捉拍照事件的SurfaceHolder.Callback对象
holder.addCallback(this);
// 设置SurfaceHolder对象的类型
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// 开厹拍照时调用该方法
public void surfaceCreated(SurfaceHolder holder)
{
// 获得Camera对象
camera = Camera.open();
try
{
// 设置用于显示拍照摄像的SurfaceHolder对象
camera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
// 释放手机摄像头
camera.release();
camera = null;
}
}
// 停止拍照时调用该方法
public void surfaceDestroyed(SurfaceHolder holder)
{
// 释放手机摄像头
camera.release();
}
// 拍照状态变化时调用该方法
public void surfaceChanged(final SurfaceHolder holder, int format,
int w, int h)
{
try
{
Camera.Parameters parameters = camera.getParameters();
// 设置照片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 根据屏幕方向设置预浏尺寸
if (getWindowManager().getDefaultDisplay().getOrientation() == 0)
parameters.setPreviewSize(h, w);
else
parameters.setPreviewSize(w, h);
// 设置拍摄照片的实际分辨率,在本例中的分辨率是1024*768
parameters.setPictureSize(1024, 768);
// 设置保存的图像大小
camera.setParameters(parameters);
// 开始拍照
camera.startPreview();
// 准备用于表示对焦状态的图像(类似对焦符号)
ivFocus.setImageResource(R.drawable.focus1);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
ivFocus.setScaleType(ScaleType.CENTER);
addContentView(ivFocus, layoutParams);
ivFocus.setVisibility(VISIBLE);
// 自动对焦
camera.autoFocus(new AutoFocusCallback()
{
public void onAutoFocus(boolean success, Camera camera)
{
if (success)
{
// success为true表示对焦成功,改变对焦状态图像(一个绿色的png图像)
ivFocus.setImageResource(R.drawable.focus2);
}
}
});
}
catch (Exception e)
{
}
}
// 停止拍照,并将拍摄的照片传入PictureCallback接口的onPictureTaken方法
public void takePicture()
{
if (camera != null)
{
camera.takePicture(null, null, pictureCallback);
}
}
}
}
public class Main extends Activity implements OnClickListener
{
public ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
btnTakePicture.setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.imageview);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1)
{
if (resultCode == 20)
{
Bitmap cameraBitmap;
byte[] bytes = data.getExtras().getByteArray("bytes");
cameraBitmap = BitmapFactory.decodeByteArray(bytes, 0,
bytes.length);
if (getWindowManager().getDefaultDisplay().getOrientation() == 0)
{
Matrix matrix = new Matrix();
matrix.setRotate(90);
cameraBitmap = Bitmap.createBitmap(cameraBitmap, 0, 0,
cameraBitmap.getWidth(), cameraBitmap.getHeight(),
matrix, true);
}
File myCaptureFile = new File("/sdcard/camera.jpg");
try
{
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(myCaptureFile));
cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
imageView.setImageBitmap(cameraBitmap);
}
catch (Exception e)
{
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void onClick(View view)
{
Intent intent = new Intent(this, CameraPreview.class);
startActivityForResult(intent, 1);
}
}
最后,需要在mainfirst中添加使用camera的权限:
<uses-permission android:name="android.permission.CAMERA" />