2014年12月3日 星期三

Android Camera part4 -- preview size

讀取目前的 preview size

Camera.getParameters().getPreviewSize() 會取得目前的 preview size,回傳 Camera.Size。

讀取所有 preview size

Camera.getParameters().getSupportedPreviewSizes() 會讀取所有支援的 preview size,回傳 List<Camera.Size>。

設定 preview size

設定 preview size 比較麻煩,要先讀取 Camera.Parameters,然後對 Camera.Parameters 設定 preview size,再把 Camera.Parameters 寫入 Camera。

  1. Camera.getParameters() 取得 Camera.Parameters。
  2. Camera.Parameters.setPreviewSize(int w, int h) 設定 preview size。
  3. Camera.setParameters() 設定 Camera.Parameters。
import java.io.IOException; import java.util.List; import android.hardware.Camera; import android.hardware.Camera.Size; import android.os.Bundle; import android.app.Activity; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.Spinner; public class CameraTestActivity extends Activity implements SurfaceHolder.Callback { private SurfaceView mSurfaceview = null; private SurfaceHolder mSurfaceHolder = null; private Camera mCamera = null; private Spinner mSpinner = null; private List<Size> mSupportedPreviewSizes = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); setContentView(ll); mSpinner = new Spinner(this); ll.addView(mSpinner); mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<!----> av, View v, int p, long id){ Camera.Parameters parameter = mCamera.getParameters(); parameter.setPreviewSize( mSupportedPreviewSizes.get(p).width, mSupportedPreviewSizes.get(p).height); mCamera.setParameters(parameter); } @Override public void onNothingSelected(AdapterView<!----> arg0) { } }); mSurfaceview = new SurfaceView(this); mSurfaceHolder = mSurfaceview.getHolder(); mSurfaceHolder.addCallback(this); ll.addView(mSurfaceview); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder arg0) { try { mCamera = Camera.open(); // set preview size list mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes(); String[] availableSizeNames = new String[mSupportedPreviewSizes.size()]; for (int i=0; i<mSupportedPreviewSizes.size(); i++) { availableSizeNames[i] = Integer.toString(mSupportedPreviewSizes.get(i).width) + " x " + Integer.toString(mSupportedPreviewSizes.get(i).height); } ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, availableSizeNames); adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); mSpinner.setAdapter(adapter); // set default preview size Size mCurrentPreviewSizes = mCamera.getParameters().getPreviewSize(); for (int i=0; i<mSupportedPreviewSizes.size(); i++) { if (mSupportedPreviewSizes.get(i).equals(mCurrentPreviewSizes)) { mSpinner.setSelection(i); break; } } // set preview parameter mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay(mSurfaceHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder arg0) { mCamera.release(); } }

沒有留言:

張貼留言