Camera control
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
/**
* 카메라 샘플
*/
public class HelloCamera extends Activity
{
// Log용 TAG
private static final String TAG = "HelloCamera";
// ImageView 설정 이미지 약하게 크기. 1 / 8의 크기로 처리
private static final int IN_SAMPLE_SIZE = 8;
// 카메라 제어
private Camera mCamera;
// 촬영 사진보기
private ImageView mImage;
// 처리중 신고
private boolean mInProgress;
// 카메라 미리보기 SurfaceView의 리스너
private SurfaceHolder.Callback mSurfaceListener = new SurfaceHolder.Callback()
{
public void surfaceCreated(SurfaceHolder holder)
{
// SurfaceView가 생성되면 카메라를 열
mCamera = Camera.open();
Log.i(TAG, "Camera opened");
// SDK1.5에서 setPreviewDisplay이 IO Exception을 throw한다
try
{
mCamera.setPreviewDisplay(holder);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// SurfaceView가 삭제되는 시간에 카메라를 개방
mCamera.release();
mCamera = null;
Log.i(TAG, "Camera released");
}
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");
}
};
// 버튼을 눌렀을 때 수신기
private View.OnClickListener mButtonListener = new View.OnClickListener()
{
public void onClick(View v)
{
if (mCamera != null && mInProgress == false)
{
// 이미지 검색을 시작한다. 리스너 설정
mCamera.takePicture(mShutterListener, // 셔터 후
null, // Raw 이미지 생성 후
mPicutureListener); // JPE 이미지 생성 후
mInProgress = true;
}
}
};
private Camera.ShutterCallback mShutterListener = new Camera.ShutterCallback()
{
// 이미지를 처리하기 전에 호출된다.
public void onShutter()
{
// 셔터 소리 재생 코드 생략
Log.i(TAG, "onShutter");
}
};
// JPEG 이미지를 생성 후 호출
private Camera.PictureCallback mPicutureListener = new Camera.PictureCallback()
{
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);
// 정지된 프리뷰를 재개
camera.startPreview();
// 처리중 플래그를 떨어뜨림
mInProgress = false;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImage = (ImageView) findViewById(R.id.image_view);
SurfaceView surface = (SurfaceView) findViewById(R.id.surface_view);
SurfaceHolder holder = surface.getHolder();
// SurfaceView 리스너를 등록
holder.addCallback(mSurfaceListener);
// 외부 버퍼를 사용하도록 설정
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 셔터의 버튼에 리스너를 등록
ImageButton button = (ImageButton) findViewById(R.id.shutter);
button.setOnClickListener(mButtonListener);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 카메라 샘플에 대한 매니 페스트 파일 -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.hews.hellocamera" android:versionCode="1" android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- 항상 가로로 표시 -->
<activity android:name=".HelloCamera" android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 카메라 사용 권한 설정 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-sdk android:minSdkVersion="3"></uses-sdk>
</manifest>
/res/layout/mail.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- main.xml 카메라 샘플 레이아웃 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<!-- 미리보기 -->
<SurfaceView
android:id="@+id/surface_view"
android:layout_gravity="center"
android:layout_height="100px"
android:layout_width="160px"/>
<!-- 셔터 버튼 -->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/shutter"
android:src="@drawable/arrow" />
<!-- 가져온 이미지를 표시 -->
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/image_view" />
</LinearLayout>
출처: