欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 照相机的实例应用

程序员文章站 2023-12-14 12:30:04
android 照相机的实例应用 关键技术: surfaceholder.callback public class mycamerademo ext...

android 照相机的实例应用

关键技术:

surfaceholder.callback

public class mycamerademo extends activity { 
  private surfaceview surface = null ; 
  private button but = null ; 
  private surfaceholder holder = null ; 
  private camera cam = null ; 
  private boolean previewrunning = true ; 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    super.setcontentview(r.layout.main); 
    this.but = (button) super.findviewbyid(r.id.but) ; 
    this.surface = (surfaceview) super.findviewbyid(r.id.surface) ; 
     
    this.holder = this.surface.getholder() ; 
    this.holder.addcallback(new mysurfaceviewcallback()) ; 
    //设置缓冲类型 
    this.holder.settype(surfaceholder.surface_type_push_buffers) ; 
    //设置分辨率 
    this.holder.setfixedsize(600, 350); 
     
    this.but.setonclicklistener(new onclicklistenerimpl()) ; 
  } 
  private class onclicklistenerimpl implements onclicklistener { 
 
    @override 
    public void onclick(view v) { 
      if(mycamerademo.this.cam != null) { 
        //自动对焦 
        mycamerademo.this.cam.autofocus(new autofocuscallbackimpl()) ; 
      } 
    } 
     
  } 
   
  private class mysurfaceviewcallback implements surfaceholder.callback { 
 
    //当预览界面格式大小改变时,调用 
    public void surfacechanged(surfaceholder holder, int format, int width, 
        int height) { 
       
    } 
 
    //初次实例化界面调用 
    public void surfacecreated(surfaceholder holder) { 
      mycamerademo.this.cam = camera.open(0) ;  // 取得第一个摄像头 
      //窗口服务 
      windowmanager manager = (windowmanager) mycamerademo.this 
          .getsystemservice(context.window_service); 
      //取得display显示对象 
      display display = manager.getdefaultdisplay() ; 
      //照相机参数 
      parameters param = mycamerademo.this.cam.getparameters() ; 
      //将照相机预览大小设置为display大小 
      param.setpreviewsize(display.getwidth(), display.getheight()) ; 
      param.setpreviewframerate(5) ; // 一秒5帧 
      param.setpictureformat(pixelformat.jpeg) ; // 图片形式 
      param.set("jpen-quality", 80) ;//图片质量,最高100 
      mycamerademo.this.cam.setparameters(param) ; 
      try { 
        mycamerademo.this.cam.setpreviewdisplay(mycamerademo.this.holder) ; 
      } catch (ioexception e) { 
      } 
      mycamerademo.this.cam.startpreview() ; // 进行预览 
      mycamerademo.this.previewrunning = true ;  // 已经开始预览 
    } 
 
    @override 
    public void surfacedestroyed(surfaceholder holder) { 
      if(mycamerademo.this.cam != null) { 
        if(mycamerademo.this.previewrunning) { 
          mycamerademo.this.cam.stoppreview() ;  // 停止预览 
          mycamerademo.this.previewrunning = false ; 
        } 
        mycamerademo.this.cam.release() ; 
      } 
    } 
     
  } 
   
  private class autofocuscallbackimpl implements autofocuscallback { 
 
    @override 
    public void onautofocus(boolean success, camera camera) { 
      if(success) {  // 成功 
        mycamerademo.this.cam.takepicture(sc, pc, jpgcall) ; 
      } 
    } 
     
  } 
   
  private picturecallback jpgcall = new picturecallback() { 
 
    @override 
    public void onpicturetaken(byte[] data, camera camera) {  // 保存图片的操作 
      bitmap bmp = bitmapfactory.decodebytearray(data, 0, data.length); 
      string filename = environment.getexternalstoragedirectory() 
          .tostring() 
          + file.separator 
          + "mldnphoto" 
          + file.separator 
          + "mldn_" + system.currenttimemillis() + ".jpg"; 
      file file = new file(filename) ; 
      if (!file.getparentfile().exists()) { 
        file.getparentfile().mkdirs() ; // 创建文件夹 
      } 
      try { 
        bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream(file)) ; 
        bmp.compress(bitmap.compressformat.jpeg, 80, bos) ; // 向缓冲区之中压缩图片 
        bos.flush() ; 
        bos.close() ; 
        toast.maketext(mycamerademo.this, 
            "拍照成功,照片已保存在" + filename + "文件之中!", toast.length_short) 
            .show(); 
      } catch (exception e) { 
        toast.maketext(mycamerademo.this, 
            "拍照失败!", toast.length_short) 
            .show(); 
      } 
      mycamerademo.this.cam.stoppreview() ; 
      mycamerademo.this.cam.startpreview() ; 
    } 
     
  } ; 
   
  private shuttercallback sc = new shuttercallback(){ 
    @override 
    public void onshutter() { 
      // 按下快门之后进行的操作 
    } 
  } ; 
  private picturecallback pc = new picturecallback() { 
 
    @override 
    public void onpicturetaken(byte[] data, camera camera) { 
       
    } 
     
  } ; 
} 

 主布局函数

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout  
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical"  
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <button 
    android:id="@+id/but"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
    android:text="照相" /> 
  <surfaceview 
    android:id="@+id/surface" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />  
</linearlayout> 

以上就是android 相机的使用方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: