CameraPreview.java
package com.CameraPR;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CameraPreview extends Activity {
private Preview mPreview;
private Button mCaptureButton;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (Preview)findViewById(R.id.preview_layer);
mCaptureButton = (Button)findViewById(R.id.capture_button);
mCaptureButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPreview.takePicture("MyPhoto", "Photo taken by sample application");
}
});
}
protected void onResume()
{
super.onResume();
mPreview.openCamera();
}
protected void onPause()
{
super.onPause();
mPreview.releaseCamera();
}
private static final int MENU_SWITCH_CAM = 0;
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0,MENU_SWITCH_CAM,Menu.NONE,"카메라 전환");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case MENU_SWITCH_CAM:
mPreview.switchCamera();
return true;
}
return false;
}
}
Preview.java
package com.CameraPR;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class Preview extends ViewGroup implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;
int camCount;
int currCamIdx = 0;
Context mContext;
private void init(Context context)
{
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
camCount = Camera.getNumberOfCameras();
CameraInfo info = new CameraInfo();
for(int i =0; i<camCount; i++)
{
Camera.getCameraInfo(i, info);
if(info.facing == CameraInfo.CAMERA_FACING_BACK)
currCamIdx = i;
}
mContext = context;
}
Preview(Context context) {
super(context);
init(context);
// TODO Auto-generated constructor stub
}
public Preview(Context context, AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
init(context);
}
public Preview(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public void takePicture(final String title, final String description)
{
mCamera.takePicture(null, null,new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
picture, title, description);
Toast.makeText(mContext, "이미지가 저장 되었습니다.", Toast.LENGTH_SHORT).show();
mCamera.startPreview();
}
});
}
public void openCamera()
{
mCamera = Camera.open(currCamIdx);
preparePreviewSize();
}
public void preparePreviewSize()
{
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes,getResources().getDisplayMetrics().widthPixels
,getResources().getDisplayMetrics().heightPixels);
}
public void switchCamera()
{
releaseCamera();
currCamIdx = (++currCamIdx)%camCount;
setCamera(Camera.open(currCamIdx));
try{
mCamera.setPreviewDisplay(mHolder);
}catch(IOException exception)
{
Log.e(TAG,"IOException caused by setPreviewDisplay()",exception);
}
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
requestLayout();
}
public void releaseCamera()
{
if(mCamera != null)
{
mCamera.startPreview();
mCamera.release();
mCamera = null;
}
}
public void setCamera(Camera camera)
{
mCamera = camera;
preparePreviewSize();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
if(getChildCount() > 0)
{
final View child = getChildAt(0);
final int width = r- l;
final int height = b - t;
int previewWidth = width;
int previewHeight = height;
if(mPreviewSize != null)
{
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
}
if(width * previewHeight > height * previewWidth)
{
final int scaledChildWidth = previewWidth * height / previewHeight;
child.layout((width - scaledChildWidth)/2, 0, (width + scaledChildWidth)/2, height);
}else
{
final int scaledChildHeight = previewHeight * width / previewWidth;
child.layout(0, (height - scaledChildHeight)/2, width, (height + scaledChildHeight));
}
}
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h)
{
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double)w/h;
if(sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for(Size size : sizes)
{
double ratio = (double)size.width/ size.height;
if(Math.abs(ratio - targetRatio)>ASPECT_TOLERANCE)
continue;
if(Math.abs(size.height - targetHeight)<minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if(optimalSize == null)
{
minDiff = Double.MAX_VALUE;
for(Size size : sizes)
{
if(Math.abs(size.height - targetHeight)<minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// TODO Auto-generated method stub
if(mCamera != null)
{
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
if(mCamera != null)
{
mCamera.setPreviewDisplay(holder);
}
}catch(IOException exception)
{
Log.e(TAG,"IOException caused by setPreviewDisplay()",exception);
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(mCamera != null)
{
mCamera.stopPreview();
}
}
}
CameraPR.java 이게 메인파일입니다.
package com.CameraPR;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CameraPR extends Activity {
private static final int PICK_FROM_ALBUM = 1;
protected static final int PICK_FROM_CAMERA = 0;
private Button cameraButton;
private Button lilbraryButton;
CameraPreview CPreview;
ImageEdit IEdit;
Activity act = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cameraButton = (Button)findViewById(R.id.btn1);
cameraButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doTakePictureAction();
}
});
lilbraryButton = (Button)findViewById(R.id.btn2);
lilbraryButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doTakeAlbumAction();
}
});
}
private void doTakeAlbumAction()
{
// 앨범 호출
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
startActivityForResult(intent, PICK_FROM_ALBUM);
}
private void doTakePictureAction()
{
Intent intent = new Intent(act,CameraPreview.class);
act.startActivityForResult(intent, PICK_FROM_CAMERA);
}
}
오류점 잡아주세요!




어떠한 오류가 뜨고 에러로그 정도 주시면 좋을거 같은데요
이래서는 머가 문제인지 알기 어렵습니다.
로그찍어가며 에러 위치를 찾는게 좋아보여요