由於筆者使用的手機比較舊,祇是 Android 4.4.4(KitKat)版本,所以祇能使用 Camera API(Camera1 / android.hardware.Camera)。新的 CameraX 和 Camera2 API 支持搭載 Android 5.0(API 級別 21)或更高版本的設備,這個是最簡單的相機程式(Simple Camera Program),程式是開啟背面的相機,然後顯示在屏幕(Preview)上,主要是用來測試相機程式是否工作?
簡單相機程式(Simple Camera Program) |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11a Camera BasicCamera
- 程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11a Camera BasicCamera
- 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本
簡單相機程式(Simple Camera Program)編程步驟:
- 設定 Camera 許可權限後 - AndroidManifest.xml
- 設定 SurfaceView 參數 - init_View()
- 啟動 SurfaceView Callback - mSurfaceHolder.addCallback(this);
- 打開 Camera - Camera.open()
- 設置 Camera 參數 - setParameters()
- StartPreview Camera 顯示 - setPreviewDisplay(surfaceHolder) → startPreview()
AndroidManifest.xml 獲取 Camera 許可權:
<uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" /> |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
<SurfaceView android:id="@+id/activity_camera_surface" android:layout_width="match_parent" android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout> |
package bugworkshop.blogspot.com.camera;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log;
// Camera import android.hardware.Camera;
// SurfaceView import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException;
public class BasicCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback{
// Camera private Camera mCamera;
// SurfaceView private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera);
init_View(); }
//@Override public void init_View() { mSurfaceView = (SurfaceView)findViewById(R.id.activity_camera_surface); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }
@Override public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) { mCamera = Camera.open(); if (mCamera != null) { Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(640, 480); p.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); mCamera.setParameters(p); } try { mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } }
@Override public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) { }
@Override public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { }
} |
沒有留言:
張貼留言