![](/bink.gif) |
|
| ![](/bink.gif) |
|
![](icon/private_name/1.gif) |
8³â Àü |
CameraEx.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
//Ä«¸Þ¶óÀÇ Á¦¾î
public class CameraEx extends Activity {
//¾ÖÇø®ÄÉÀÌ¼Ç ÃʱâÈ
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new CameraView(this));
}
}
CameraView.java
import android.content.Context;
import android.hardware.Camera;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.FileOutputStream;
//Ä«¸Þ¶óÀÇ Á¦¾î
public class CameraView extends SurfaceView
implements SurfaceHolder.Callback {
private SurfaceHolder holder;//Ȧ´õ
private Camera camera;//Ä«¸Þ¶ó
//»ý¼ºÀÚ
public CameraView(Context context) {
super(context);
// SurfaceHolder »ý¼º
holder=getHolder();
holder.addCallback(this);
// Push Buffer ÁöÁ¤
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
//Surface »ý¼º½Ã È£Ãâ
public void surfaceCreated(SurfaceHolder holder) {
//Ä«¸Þ¶óÀÇ ÃʱâÈ
try {
camera=Camera.open();
camera.setPreviewDisplay(holder);
} catch (Exception e) {
}
}
//Surface º¯°æ À̺¥Æ® ó¸®
public void surfaceChanged(SurfaceHolder holder,int format,int w,int h) {
// Ä«¸Þ¶óÀÇ ¸®ºä °³½Ã
Camera.Parameters parameters=camera.getParameters();
parameters.setPreviewSize(w,h);
camera.setParameters(parameters);
camera.startPreview();
}
//Surface ÇØÁ¦ À̺¥Æ® ó¸®
public void surfaceDestroyed(SurfaceHolder holder) {
//Ä«¸Þ¶ó ÇÁ¸®ºäÀÇ °³½Ã
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera=null;
}
//ÅÍÄ¡ À̺¥Æ® ó
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
takePicture();
camera.startPreview();
}
return true;
}
//»çÁø ÃÔ¿µ
public void takePicture() {
//Ä«¸Þ¶óÀÇ ½ºÅ©¸°¼¦ Ãëµæ
camera.takePicture(null,null,new Camera.PictureCallback() {
public void onPictureTaken(byte[] data,Camera camera) {
try {
data2sd(getContext(),data,"test.jpg");
} catch (Exception e) {
android.util.Log.e("",""+e.toString());
}
}
});
}
//¹ÙÀÌÆ® µ¥ÀÌÅÍ -> SD Ä«µå
private static void data2sd(Context context,
byte[] w,String fileName) throws Exception {
//SDÄ«µåÀÇ µ¥ÀÌÅÍ ÀúÀå
FileOutputStream fos=null;
try {
fos=new FileOutputStream("/sdcard/"+fileName);
fos.write(w);
fos.close();
} catch (Exception e) {
if (fos!=null) fos.close();
throw e;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.npaka.cameraex"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CameraEx"
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" />
</manifest>
|
|
̵̧ : 261 |
̵̧
¸ñ·Ï
|
|
| ![](/bink.gif) |
|