资源简介
Android开发之拍照功能实现(附源码)
代码片段和文件信息
package org.winplus.camera;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.View.onclickListener;
import android.widget.Button;
public class CameraDemoActivity extends Activity{
private final static String TAG = “CameraActivity“;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
private File picture;
private Button btnSave;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.requestWindowFeature(Window.FEATURE_NO_title);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
surfaceView = (SurfaceView) findViewById(R.id.camera_preview); // Camera interface to instantiate components
surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components
surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btnSave = (Button) findViewById(R.id.save_pic);
btnSave.setonclickListener(new onclickListener() {
@Override
public void onclick(View v) {
takePic();
}
});
}
@Override
public boolean onkeydown(int keyCode KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CAMERA
|| keyCode == KeyEvent.KEYCODE_SEARCH) {
takePic();
return true;
}
return super.onkeydown(keyCode event);
}
private void takePic() {
camera.stopPreview();// stop the preview
camera.takePicture(null null pictureCallback); // picture
}
// Photo call back
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
//@Override
public void onPictureTaken(byte[] data Camera camera) {
new SavePictureTask().execute(data);
camera.startPreview();
}
};
// save pic
class SavePictureTask extends AsyncTask {
@Override
protected String doInBackground(byte[]... params) {
String fname = DateFormat.format(“yyyyMMddhhmmss“ new Date()).toString()+“.jpg“;
Log.i(TAG “fname=“+fname+“;dir=“+Environment.getExternalStorageDirectory());
//picture = new File(Environment.getExternalStorageDirectory()fname);// create file
picture = new File(Environment.getExternalStorageDirectory()+“/“+fname);
try {
FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream
fos.write(params[0]); // Written to the file
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 364 2011-11-29 15:53 CameraDemo\.classpath
文件 846 2011-11-29 15:53 CameraDemo\.project
文件 1614 2011-12-03 10:04 CameraDemo\AndroidManifest.xm
文件 1248 2011-11-29 15:53 CameraDemo\proguard.cfg
文件 361 2011-11-29 15:53 CameraDemo\project.properties
文件 4147 2011-11-29 15:53 CameraDemo\res\drawable-hdpi\ic_launcher.png
文件 1723 2011-11-29 15:53 CameraDemo\res\drawable-ldpi\ic_launcher.png
文件 2574 2011-11-29 15:53 CameraDemo\res\drawable-mdpi\ic_launcher.png
文件 752 2011-12-03 10:00 CameraDemo\res\layout\main.xm
文件 163 2011-12-03 10:00 CameraDemo\res\values\strings.xm
文件 4330 2011-12-03 10:04 CameraDemo\src\org\winplus\camera\CameraDemoActivity.java
目录 0 2011-12-03 10:04 CameraDemo\src\org\winplus\camera
目录 0 2011-12-03 10:04 CameraDemo\src\org\winplus
目录 0 2011-12-03 10:04 CameraDemo\res\drawable-hdpi
目录 0 2011-12-03 10:04 CameraDemo\res\drawable-ldpi
目录 0 2011-12-03 10:04 CameraDemo\res\drawable-mdpi
目录 0 2011-12-03 10:04 CameraDemo\res\layout
目录 0 2011-12-03 10:04 CameraDemo\res\values
目录 0 2011-12-03 10:04 CameraDemo\src\org
目录 0 2011-12-03 10:04 CameraDemo\assets
目录 0 2011-12-03 10:04 CameraDemo\res
目录 0 2011-12-03 10:04 CameraDemo\src
目录 0 2011-12-03 10:04 CameraDemo
----------- --------- ---------- ----- ----
18122 23
评论
共有 条评论