Android多媒體應用開發-控制攝像頭拍照
現在的手機的功能可謂是五花八門,手機照相功能就是特別突出的一點,現在的手機照相機甚至成了專業數碼照相機,可以拍攝出清晰的照片和錄制高分辨率的視頻。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" />