2010年12月16日星期四

Android的照相機預覽功能(Camera Preview)

這是一個最簡單的照相機預覽功能的示範: 在佈局設置一個SurfaceView提供預覽功能; 本應用程式只能預覽, 並不支持其他如拍照等功能.

在Android模擬器上運行
在真正的Android移動電話上運行

首先, 修改AndroidManifest.xml授予使用照相機權限, "android.permission.CAMERA".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidCameraPreview"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidCameraPreview"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
</manifest>


修改佈局(main.xml), 添加一個SurfaceView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SurfaceView
android:id="@+id/previewsurface"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


主要代碼, AndroidCameraPreview.java
package com.AndroidCameraPreview;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AndroidCameraPreview extends Activity implements SurfaceHolder.Callback{

Camera myCamera;
SurfaceView previewSurfaceView;
SurfaceHolder previewSurfaceHolder;
boolean previewing = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

getWindow().setFormat(PixelFormat.UNKNOWN);
previewSurfaceView = (SurfaceView)findViewById(R.id.previewsurface);
previewSurfaceHolder = previewSurfaceView.getHolder();
previewSurfaceHolder.addCallback(this);
previewSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

if(previewing){
myCamera.stopPreview();
previewing = false;
}


try {
myCamera.setPreviewDisplay(arg0);
myCamera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
myCamera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
myCamera.stopPreview();
myCamera.release();
myCamera = null;
previewing = false;
}
}


主活動(AndroidCameraPreview)定義為extends Activity implements SurfaceHolder.Callback, 所以,我們必須重寫surfaceCreated(), surfaceChanged()和surfaceDestroyed()方法. 這裡亦是實現照相機預覽功能的主要部分.
以及, 需要在onCreate()方法中先設置我們的SurfaceHolder.


相關文章:
- 照相機預覽功能(Camera Preview)畫面轉了90度!!!
- 實現Android照相機的拍照功能(Camera.takePicture)



8 則留言:

  1. 感謝你的分享
    但是我遇到一個問題
    使用您這個程式碼時
    實際跑在desire上
    出現的preview畫面卻是比人眼看到的實際畫面整個轉了90度
    不知道是什麼原因?

    回覆刪除
  2. mayru,

    I have no problem on Wildfire.

    Can you try to disable phone's Auto-Orientation?
    In Phone Home Screen -> MENU -> Settings -> Sound & display -> Un-Check Orientation under Display Settings.

    To see if it's OK.

    Do you phone display correct on other Camera App?

    Thx.

    回覆刪除
  3. hi mayru,

    camera preview is not standardized, some Android devices might show the preview sideways (as your case).

    You can add the code in your onCreate()
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    回覆刪除
  4. 柏樺,
    Have you 修改AndroidManifest.xml授予使用照相機權限, "android.permission.CAMERA"?

    I run on Wildfire also.

    回覆刪除
  5. 您好 我測試是用sony xperia s, 但是實做出來 畫面呈現的部分 會失真

    請問作者有此問題嗎?

    再次感謝分享

    回覆刪除
  6. 你是用開發環境嗎測試媽

    回覆刪除