首先,Camera中,调用系统相机:
使用系统Intent,ACTION_IMAGE_CAPTURE
注册Camera功能
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
调用系统的Camera功能,由于默认返回的是缩略图,以下方法是通过将拍的照片存入自定义的SD卡中,之后再取出到ImageView中,使得返回的为原图:
MyFilePath = Environment.getExternalStorageDirectory().getPath();
MyFilePath = MyFilePath + "/" + "example.png";
public void StartCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(new File(MyFilePath));
//将系统默认保存路径改为指定的sd卡中的路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, requestCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST){
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap)bundle.get("data");
mImageView.setImageBitmap(bitmap);
}else{
FileInputStream fis = null;
try{
fis = new FileInputStream(MyFilePath);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
mImageView.setImageBitmap(bitmap);
//这样显示的就是原图,而不是缩略图
}catch(FileNotFoundException e){
e.printStackTrace();
}finally{
try{
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
以下就是自定义相机的基础实现
首先需要注意,由于是自定义的相机,就需要添加相机权限:
<uses-permission android:name="android.permission.CAMERA">
然后,即开始创建自定义的相机:
public class CustomCamera extends Activity implements SurfaceHolder.Callback{
private Camera mCamera;
private SurfaceView mPreview;//要实现SurfaceHolder.Callback接口中的方法
private SurfaceHolder mHolder;
private Camera.PictureCallback mpictureCallback = new Camera.PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera) {
File tempFile = new File("/sdcard/temp.png")
try{
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(data);
fos.close();
Intent intent = new Intent(CustomCamera.this, ResultAty.class);
Intent.putExtra("picPath", temFile.getAbsolutePath());
startActivity(intent);
CustomCamera.this.finish();
}catch(Exception e){
e.printStackTrace();
}
}
};
protected void OnCreate(Bundle savedInstanceState){
super.OnCreate(savedInstanceState);
setContentView(R.layout.custom);
mPreview = (SurfaceView)findViewById(R.id.preview);
mHolder = mPreview.getHolder();
mHolder.addCallback(this);
mPreview.setOnClickListener(new View.onClickListener(){
public void onClick(View v){
mCamera.autoFocus(null);
}
});
}
public void capturePhoto(View view){
Camera.parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPreviewSize(800,400);//调整拍照的显示效果
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.autoFocus(new Camera.AutoFocusCallback(){
public void onAutoFocus(boolean success, Camera camera){
if(success){
mCamera.takePicture(null, null, mPictureCallback);
}
}
});
}
//------------------------
protected void onResume(){
super.onResume();
if(mCamera == null){
mCamera = getCamera();
if(mHolder != null){
setStartPreview(mCamera, mHolder);
}
}
}
protected void onPause(){
super.onPause();
releaseCamera();
}
//----------通过重写这两个方法,使得Camera生命周期与Activity周期进行绑定
//以保证资源能够被正确的初始化和释放
//--------创建了Camera生命周期的三个方法:
//获取Camera对象,同时进行初始化操作
private Camera getCamera(){
Camera camera;
//在Android5.0之后系统推荐使用Camera2
//但不使用高级的功能时,Camera仍然适用
//在此是最基本的Camera操作
try{
camera = Camera.open();//进行初始化操作
}catch(Exception e){
camera = null;
e.printStackTrace();
}
return camera;
}
//开始预览相机内容
private void setStartPreview(Camera camera, SurfaceHolder holder){
try{
camera.setPreviewDisplay(holder);//将Camera与SurfaceView进行关联
//同时Camera进行取景,预览画面
camera.setDisplayOrientation(90);//将Camera预览旋转90度
camera.startPreview();
}catch(IOException e){
e.printStackTrace();
}
}
//释放相机资源,同时解除与SurfaceView的绑定
private void releaseCamera(){
if(mCamera != null){
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
//---------至此,camera生命周期的方法创建完毕
//-----以下将Camera生命周期与SurfaceView进行绑定
public void surfaceCreated(SurfaceHolder holder){
setStartPreview(mCamera, mHolder);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
mCamera.stopPreview();
setStartPreview(mCamera, mHolder);
}
public void surfaceDestroyed(SurfaceHolder holder){
releaseCamera();
}
//------------对相机的生命周期的管理
//还需要另外一个类:
public class ResultAty extends Activity{
protected void OnCreate(Bundle savedInstanceState){
super.OnCreate(savedInstanceState);
setContentView(R.layout.result);
String path = getIntent().getStringExtra("picPath");
ImageView imageView = (ImageView)findViewById(R.id.pic);
// Bitmap bitmap = BitmapFactory.decodeFile(path);
// imageView.setImageBitmap(bitmap);
try{
FileInputStream fis = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
Matrix matrix = new Matrix();
matrix.setRotate(90);//取得的照片进行旋转
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(bitmap);
}catch(Exception e){
e.printStackTrace();
}
}
}
另外,若想要给相机添加一个水印功能,只需要在xml文件中,在<SurfaceView>标签中添加<TextView>标签即可,很简单,不予演示了。