关闭

Android Camera 实例

1367人阅读 评论(0) 收藏 举报
Android Camera 实例
2010-07-04 12:24
注:使用摄像头等媒体设备时必须连上存储卡
<uses-permission android:name="android.permission.CAMERA" />
CamMain.java:
public class CamMain extends Activity implements SurfaceHolder.Callback {
//在SurfaceView上绘制应该实现SurfaceHolder.Callback接口
private SurfaceHolder holder=null;
private SurfaceView surface=null;
private Button bt=null;
private Camera cam=null;

private ShutterCallback sc=new ShutterCallback(){
         @Override
         public void onShutter() {
          // 按下快门后的回调函数
         }};
  
private PictureCallback pc=new PictureCallback(){
         @Override
         public void onPictureTaken(byte[] arg0, Camera arg1) {
          // 源数据
         }};
  
private PictureCallback jpgcall=new PictureCallback(){
         @Override
         public void onPictureTaken(byte[] b, Camera c) {
          // TODO Auto-generated method stub
          try{
           Bitmap bmp=BitmapFactory.decodeByteArray(b, 0, b.length);
           File f=new File("/mnt/sdcard/photo.jpg");
           BufferedOutputStream bos=new BufferedOutputStream(
             new FileOutputStream(f));
           bmp.compress(Bitmap.CompressFormat.JPEG, 80, bos);
           bos.flush();
           bos.close();
          }catch(Exception e){
          }
         }};
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              bt=(Button)findViewById(R.id.bt);
              bt.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
           // TODO Auto-generated method stub
           if(cam!=null){
            cam.takePicture(sc, pc, jpgcall);
            cam.stopPreview();
           }
          }});
              surface=(SurfaceView)findViewById(R.id.sv);
              holder=surface.getHolder();//设置Holder
              holder.addCallback(this);
              holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
         int height) {
        // TODO Auto-generated method stub
        try{
         Parameters p=cam.getParameters();
         p.setPictureSize(width, height);
         cam.setParameters(p);
         cam.setPreviewDisplay(holder);
         cam.startPreview();
        }catch(Exception e){

        }
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        cam=Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
  
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       >
<SurfaceView 
       android:id="@+id/sv"
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       />
<Button
android:id="@+id/bt"
android:text="DONE"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:285083次
    • 积分:3270
    • 等级:
    • 排名:第10794名
    • 原创:4篇
    • 转载:264篇
    • 译文:1篇
    • 评论:22条
    最新评论