android 摄像头的调用

时间:2021-03-09 06:08:44

这几天一直在看关于android调用摄像头的事情。其中一种是直接调用系统的摄像头,还有就是利用camera api来实现摄像头的调用。
(1) 直接调用系统的摄像头:主要就是这三句话:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent,SystemCapture);

public class MainActivity extends Activity {

private Button button;//拍摄按钮
protected int SystemCapture;

private ImageView imageView;
String imgPath = "/storage/sdcard0/mahc/image/img.jpg";
//固定的存储文件的路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

File file = new File(imgPath);
//如果文件不存在,则会自动创建文件用于存错照片,但是当这张照片有了之后,就不能进行存储别的照片了()
if(!file.exists()){

File vFile = file.getParentFile();
vFile.mkdirs();

Uri uri = Uri.fromFile(vFile);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
startActivityForResult(intent, SystemCapture);
}
}
});

}

//重载onActivityResult()函数,用于当在次进行拍照的时候显示前一次拍摄的照片的内容。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
imageView.setImageURI(Uri.fromFile(new File(imgPath)));
}
}
@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);
}
}

(2 )调用camera api,自己实现拍照功能。
主要就是预览,拍照,然后在回到预览。
主要用到的有控件有surfaceView ,surfaceHolder,
主要代码为:
.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".CameraActivity" >


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<SurfaceView
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal" />

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<Button
android:id="@+id/camera_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照" />

</LinearLayout>
</RelativeLayout>

主要的实现代码:

public class CameraActivity01 extends Activity{

private SurfaceView surfaceView;//用于预览时显示
private Button saveButton;//拍摄按钮
private Camera camera;

private SurfaceHolder surfaceHolder;//容器

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
saveButton = (Button)findViewById(R.id.camera_save);
surfaceView = (SurfaceView)findViewById(R.id.camera_preview);
//访问surfaceView的底层图形是通过surfaceHolder接口来实现的,通过getHolder()方法可以得到这个surfaceHolder对象
surfaceHolder = surfaceView.getHolder();
//通过surfaceHolder.addCallback()方法来实现图像的预览与修改
surfaceHolder.addCallback(callback);
//进行点击事件的监听
saveButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
//进行拍摄
camera.takePicture(null, null, pictureCallback);
}
});
}
SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {

@Override
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
// TODO Auto-generated method stub
//停止预览,释放camera
camera.stopPreview();
camera.release();
camera = null;
}

@Override
public void surfaceCreated(SurfaceHolder surfaceholder) {
// TODO Auto-generated method stub
//打开摄像头准备进行预。
camera = Camera.open();
try {

} catch (Exception e) {
// TODO: handle exception
camera.release();
camera = null;
}
}

@Override
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
// TODO Auto-generated method stub
//对摄像头进行参数的设置。
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(surfaceholder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
camera.release();
camera = null;
}
//开始预览
camera.startPreview();
}
};

//拍摄回调
private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
//存储文件
new SavePictureTask().execute(data);
//回到预览设置
camera.startPreview();

}
};

//异步存储文件
class SavePictureTask extends AsyncTask<byte[], String, String>{

@Override
protected String doInBackground(byte[]... params) {
// TODO Auto-generated method stub
//手机不同,存储的文件路径可能也不同,可以按照自己的手机存储路径进行文件的存储。
File picture = new File("/storage/sdcard0/mhc.jpg");
try {
FileOutputStream filo = new FileOutputStream(picture.getPath());
//filo.write(params[0]);
filo.write(params[0]);
filo.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
CameraActivity01.this.finish();
return null;
}
}
}

接着就是对文件的androidManifest.xml文件进行权限的设置:

 <uses-permission android:name="android.permission.CAMERA" />
<!--存储权限 SD卡读写权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--Camera Feature 声明对camera feature的使用 -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />

这里有个从别人哪里下载下来的完整的拍照和摄影的demo,http://download.csdn.net/detail/duolezengjie/9310685