내장 카메라 어플 불러쓸려다가 직접 프리뷰로 만들게 됬는데 실 기기에서 돌려보니깐 에러가 나더군요...
surfaceview에서 문제가 생긴거 같은데 원인을 모르겠습니다..ㅠㅠ 카메라부분좀 잘 아시는분좀 설명좀 해주세요..
package net.cameraview;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
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.hardware.Camera.Size;
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 cameraview extends Activity {
MyCameraSurface mSurface;
Button mShu;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurface=(MyCameraSurface)findViewById(R.id.surfaceView1);
//오토포커스 시작
findViewById(R.id.button1).setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
mShu.setEnabled(false);
mSurface.mCamera.autoFocus(mAutoFocus);
}
});
//촬영
mShu=(Button)findViewById(R.id.button2);
mShu.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
mSurface.mCamera.takePicture(null, null, mPicture);
}
});
}
//포커싱 성공시 촬영
AutoFocusCallback mAutoFocus = new AutoFocusCallback(){
public void onAutoFocus(boolean successs, Camera camera){
mShu.setEnabled(successs);
}
};
//사진 저장
PictureCallback mPicture = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = sd + "/cameratest.jpg";
File file = new File(path);
try{
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
}catch(Exception e){
Toast.makeText(cameraview.this, "파일 저장중 에러 : " + e.getMessage(),0).show();
return;
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.parse("file://"+path);
intent.setData(uri);
sendBroadcast(intent);
Toast.makeText(cameraview.this, "사진 저장 완료 : "+path, 0).show();
mSurface.mCamera.startPreview();
}
};
}
//미리보기 surface 클래스
class MyCameraSurface extends SurfaceView implements SurfaceHolder.Callback{
Camera mCamera;
SurfaceHolder mHolder;
public MyCameraSurface(Context context, AttributeSet attrs){
super(context, attrs);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
//surfacer 생성시 카메라 오픈, 미리보기 설정
public void surfaceCreated(SurfaceHolder holder){
mCamera = Camera.open();
try{
mCamera.setPreviewDisplay(mHolder);
}catch(IOException e){
mCamera.release();
mCamera = null;
}
}
//surface 파괴시 카메라도 파괴
public void surfaceDestroyed(SurfaceHolder holder){
if(mCamera != null){
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
//surface크기가 결정되면 최적의 미리보기 크기 설정
public void surfaceChange(SurfaceHolder holder, int format, int width, int height){
Camera.Parameters params = mCamera.getParameters();
List<Size> arSize = params.getSupportedPreviewSizes();
if(arSize == null){
params.setPreviewSize(width, height);
}else{
int diff = 10000;
Size opti = null;
for(Size s : arSize){
if(Math.abs(s.height - height)<diff){
diff = Math.abs(s.height - height);
opti = s;
}
}
params.setPreviewSize(opti.width, opti.height);
}
mCamera.setParameters(params);
mCamera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}