讀取目前的 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。
- Camera.getParameters() 取得 Camera.Parameters。
- Camera.Parameters.setPreviewSize(int w, int h) 設定 preview size。
- 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 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 adapter =
new ArrayAdapter(
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
沒有留言:
張貼留言