우선 xml 입니다
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.example.cameratest2.MyCameraSurface
android:id="@+id/previewFrame"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Capture" />
</RelativeLayout>
java 파일입니다
package com.example.cameratest2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button mShutter;
MyCameraSurface mSurface;
String mRootPath;
static final String PICFOLDER = "CameraTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurface = (MyCameraSurface)findViewById(R.id.previewFrame);
mShutter = (Button)findViewById(R.id.button1);
mShutter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mSurface.mCamera.autoFocus(mAutoFocus);
}
});
//저장할 공간 /mnt/sdcard/CameraTest 이렇게 폴더 안에 파일이 생성된다
mRootPath = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/" + PICFOLDER;
File fRoot = new File(mRootPath);
if (fRoot.exists() == false) {
if (fRoot.mkdir() == false) {
Toast.makeText(this, "사진을 저장할 폴더가 없습니다.", 1).show();
finish();
return;
}
}
}
// 포커싱 성공하면 촬영 허가
AutoFocusCallback mAutoFocus = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
mShutter.setEnabled(success);
mSurface.mCamera.takePicture(null, null, mPicture);
}
};
// 사진 저장.
PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//날짜로 파일 이름 만들기
Calendar calendar = Calendar.getInstance();
String FileName = String.format("SH%02d%02d%02d-%02d%02d%02d.jpg",
calendar.get(Calendar.YEAR) % 100, calendar.get(Calendar.MONTH)+1,
calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
String path = mRootPath + "/" + FileName;
File file = new File(path);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
} catch (Exception e) {
return;
}
//파일을 갤러리에 저장
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.parse("file://" + path);
intent.setData(uri);
sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "사진이 저장 되었습니다", 0).show();
camera.startPreview();
}
};
}
class MyCameraSurface extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
public MyCameraSurface(Context context, AttributeSet attrs) {
super(context, attrs);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// 표면 생성시 카메라 오픈하고 미리보기 설정
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
// 표면 파괴시 카메라도 파괴한다.
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
// 표면의 크기가 결정될 때 최적의 미리보기 크기를 구해 설정한다.
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
params.setPreviewSize(width, height);
params.setPictureSize(width, height);
params.setRotation(90);
mCamera.setParameters(params);
mCamera.startPreview();
}
}
'안드로이드' 카테고리의 다른 글
[android] bitmap 이미지 파일로 저장 (0) | 2013.02.28 |
---|---|
[android] camera preview capture (0) | 2013.02.28 |
[android] 화면 터치 이벤트 처리 (0) | 2013.02.27 |
[android]mac에서 android ndk 셋팅 방법 (0) | 2013.02.20 |
[android] javaCV 셋팅 방법 (0) | 2013.02.15 |