现在的手机的功能可谓是五花八门,手机照相功能就是特别突出的一点,现在的手机照相机甚至成了专业数码照相机,可以拍摄出清晰的照片和录制高分辨率的视频。Android操作系统呢,提供了相应的功能来控制拍照:
通过Camera进行拍照
1.调用Camera的open()方法打开相机。
2.调用Camera的setParameters()方法获取拍照参数。该方法返回一个Camera.Parameters对象。
3.调用Camera.Paramers对象方法设置拍照参数
4.调用Camera的startPreview()方法开始预览取景,在预览取景之前需要调用Camera的setPreViewDisplay(SurfaceHolder holder)方法设置使用哪个SurfaceView来显示取景图片。
5.调用Camera的takePicture()方法进行拍照.
6.结束程序时,调用Camera的StopPriview()结束取景预览,并调用release()方法释放资源.
创建项目:CaptureImage
运行效果:
程序代码:
==>CaptureImage.java
- package org.wwj.sound;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.hardware.Camera.PictureCallback;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.Display;
- import android.view.KeyEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceHolder.Callback;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.EditText;
- import android.widget.ImageView;
- public class CaptureImage extends Activity
- {
- SurfaceView sView;
- SurfaceHolder surfaceHolder;
- int screenWidth, screenHeight;
- // 定义系统所用的照相机
- Camera camera;
- //是否在浏览中
- boolean isPreview = false;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- // 设置全屏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- WindowManager wm = (WindowManager) getSystemService(
- Context.WINDOW_SERVICE);
- Display display = wm.getDefaultDisplay();
- // 获取屏幕的宽和高
- screenWidth = display.getWidth();
- screenHeight = display.getHeight();
- // 获取界面中SurfaceView组件
- sView = (SurfaceView) findViewById(R.id.sView);
- // 获得SurfaceView的SurfaceHolder
- surfaceHolder = sView.getHolder();
- // 为surfaceHolder添加一个回调监听器
- surfaceHolder.addCallback(new Callback()
- {
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height)
- {
- }
- public void surfaceCreated(SurfaceHolder holder)
- {
- // 打开摄像头
- initCamera();
- }
- public void surfaceDestroyed(SurfaceHolder holder)
- {
- // 如果camera不为null ,释放摄像头
- if (camera != null)
- {
- if (isPreview)
- camera.stopPreview();
- camera.release();
- camera = null;
- }
- }
- });
- // 设置该SurfaceView自己不维护缓冲
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- private void initCamera()
- {
- if (!isPreview)
- {
- camera = Camera.open();
- }
- if (camera != null && !isPreview)
- {
- try
- {
- Camera.Parameters parameters = camera.getParameters();
- // 设置预览照片的大小
- parameters.setPreviewSize(screenWidth, screenHeight);
- // 每秒显示4帧
- parameters.setPreviewFrameRate(4);
- // 设置图片格式
- parameters.setPictureFormat(PixelFormat.JPEG);
- // 设置JPG照片的质量
- parameters.set("jpeg-quality", 85);
- //设置照片的大小
- parameters.setPictureSize(screenWidth, screenHeight);
- camera.setParameters(parameters);
- //通过SurfaceView显示取景画面
- camera.setPreviewDisplay(surfaceHolder);
- // 开始预览
- camera.startPreview();
- // 自动对焦
- camera.autoFocus(null);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- isPreview = true;
- }
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- switch (keyCode)
- {
- // 当用户单击照相键、*键时执行拍照
- case KeyEvent.KEYCODE_DPAD_CENTER:
- case KeyEvent.KEYCODE_CAMERA:
- if (camera != null && event.getRepeatCount() == 0)
- {
- // 拍照
- camera.takePicture(null, null , myjpegCallback);
- return true;
- }
- break;
- }
- return super.onKeyDown(keyCode, event);
- }
- PictureCallback myjpegCallback = new PictureCallback()
- {
- public void onPictureTaken(byte[] data, Camera camera)
- {
- // 根据拍照所得的数据创建位图
- final Bitmap bm = BitmapFactory.decodeByteArray(data
- , 0, data.length);
- // 加载/layout/save.xml文件对应的布局资源
- View saveDialog = getLayoutInflater().inflate(
- R.layout.save, null);
- final EditText photoName = (EditText) saveDialog
- .findViewById(R.id.photo_name);
- // 获取saveDialog对话框上的ImageView组件
- ImageView show = (ImageView) saveDialog.findViewById(R.id.show);
- // 显示刚刚拍得的照片
- show.setImageBitmap(bm);
- //使用对话框显示saveDialog组件
- new AlertDialog.Builder(CaptureImage.this)
- .setView(saveDialog)
- .setPositiveButton("保存", new OnClickListener()
- {
- public void onClick(DialogInterface dialog,
- int which)
- {
- // 创建一个位于SD卡上的文件
- File file = new File(Environment.getExternalStorageDirectory()
- , photoName.getText().toString() + ".jpg");
- FileOutputStream outStream = null;
- try
- {
- // 打开指定文件对应的输出流
- outStream = new FileOutputStream(file);
- // 把位图输出到指定文件中
- bm.compress(CompressFormat.JPEG, 100, outStream);
- outStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- })
- .setNegativeButton("取消", null)
- .show();
- //重新浏览
- camera.stopPreview();
- camera.startPreview();
- isPreview = true;
- }
- };
- }
设置权限:
- <!-- 授予程序使用摄像头的权限 -->
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-feature android:name="android.hardware.camera" />
- <uses-feature android:name="android.hardware.camera.autofocus" />