2010年4月3日土曜日

Android カメラに autofocus を実装する。

google先生にお尋ねしても、わかりやすいサイトが
なかったので、書きました。

カメラ部分は木南さんの本を参考にしています。
『Google-Androidアプリケーション開発入門』
ありがとうございます!!
買った方がいいですよ~。

シャッターボタンを押されたときに
Camera.autoFocus(Camera.AutoFocusCallback);
を呼んで,
Camera.AutoFocusCallback
で、autofocus を切って、takePicture すればOK

そうそう、カメラを使う場合には、manifest に

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

が必要です~。



public class MyPic extends Activity {

private static final String TAG = "MyPic";
private static final int IN_SAMPLE_SIZE = 1;

private Camera mCamera;
private ImageView mImage;
private boolean mInProgress;

private SurfaceHolder.Callback mSurfaceListener =
new SurfaceHolder.Callback() {

@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
Log.i(TAG, "Camera opened");
try {
mCamera.setPreviewDisplay(holder);
}
catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
mCamera = null;
Log.i(TAG, "Camera released");
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(width, height);
mCamera.setParameters(parameters);
mCamera.startPreview();
Log.i(TAG, "Camera preview started");
Log.i(TAG, "width : " + width);
Log.i(TAG, "height : " + height);
}
};

private Camera.AutoFocusCallback mAutoFocusListener =
new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Log.i(TAG,"AutoFocus : " + success);
camera.autoFocus(null);
camera.takePicture(mShutterListener, null, mPictureListener);
mInProgress = true;
}
};

private Camera.ShutterCallback mShutterListener =
new Camera.ShutterCallback() {
@Override
public void onShutter() {
Log.i(TAG, "onShutter");
}
};

private View.OnClickListener mButtonListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mCamera != null && mInProgress == false){
mCamera.autoFocus(mAutoFocusListener);
}
}
};

private Camera.PictureCallback mPictureListener =
new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Picture taken");
if(data != null) {
Log.i(TAG, "JPEG Picture Taken");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = IN_SAMPLE_SIZE;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

mImage.setImageBitmap(bitmap);
mImage.setVisibility(View.VISIBLE);
}
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera2);

mImage = (ImageView) findViewById(R.id.handpreview);
SurfaceView surface = (SurfaceView) findViewById(R.id.surfaceview);
SurfaceHolder holder = surface.getHolder();

holder.addCallback(mSurfaceListener);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

ImageButton button = (ImageButton) findViewById(R.id.shutter);
button.setOnClickListener(mButtonListener);
}
}

0 件のコメント:

コメントを投稿