码迷,mamicode.com
首页 > 移动开发 > 详细

Android Camera(二)

时间:2015-09-11 15:51:30      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

上次已经写过SurfaceView显示Camera摄像了,代码可以运行,但是里面有问题,这次纠正过来,顺便实现变焦:

代码:

 

  1. public class CameravedioActivity extends Activity{  
  2.       
  3.     private static final String TAG = "CameravedioActivity";  
  4.     private Camera camera;   
  5.     private boolean preview  = false ;  
  6.     private SeekBar mSeekBar;  
  7.     private SurfaceView surfaceView;  
  8.     private SurfaceHolder surfaceHolder;  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.           
  13.        /* 
  14.         *设置窗口属性:一定要在 setContentView(R.layout.main) 之前 
  15.         */  
  16. // 窗口标题,其实可以在manifes文件里面注册  
  17. //     requestWindowFeature(Window.FEATURE_NO_TITLE);  
  18. // 全屏   
  19.        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  20.        setContentView(R.layout.main);  
  21.        surfaceView =  (SurfaceView) findViewById(R.id.surfaceView);  
  22.        surfaceHolder = surfaceView.getHolder();  
  23.        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  24.        surfaceView.getHolder().setFixedSize(200, 200);  
  25.        surfaceView.getHolder().addCallback(new SurfaceViewCallback());  
  26.          
  27.        /** 
  28.         * seekbar 用来实现变焦逻辑 
  29.         */  
  30.        mSeekBar = (SeekBar)findViewById(R.id.seekbar);  
  31.        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){  
  32.   
  33.         @Override  
  34.         public void onProgressChanged(SeekBar seekBar, int progress,  
  35.                 boolean fromUser) {  
  36.             // TODO Auto-generated method stub  
  37.             Log.d(TAG, "progress:"+progress);  
  38. //          sdk 文档描述 set zoom 之前先check   
  39.             if(camera.getParameters().isZoomSupported()){  
  40.                   
  41.                 Parameters params = camera.getParameters();  
  42.                 params.setZoom(progress);  
  43.                 camera.setParameters(params);  
  44.             }  
  45.         }  
  46.   
  47.         @Override  
  48.         public void onStartTrackingTouch(SeekBar seekBar) {  
  49.             // TODO Auto-generated method stub  
  50.             Log.d(TAG, "onStartTrackingTouch");  
  51.         }  
  52.   
  53.         @Override  
  54.         public void onStopTrackingTouch(SeekBar seekBar) {  
  55.             // TODO Auto-generated method stub  
  56.             Log.d(TAG, "onStartTrackingTouch");  
  57.         }  
  58.              
  59.        });  
  60.     }  
  61.       
  62.       
  63.       
  64.     @Override  
  65.     protected void onResume() {  
  66.         // TODO Auto-generated method stub  
  67.         super.onResume();  
  68.   
  69.     }  
  70.   
  71.     public static void setCameraDisplayOrientation(Activity activity,  
  72.             int cameraId, android.hardware.Camera camera) {  
  73.         android.hardware.Camera.CameraInfo info =  
  74.                 new android.hardware.Camera.CameraInfo();  
  75.         android.hardware.Camera.getCameraInfo(cameraId, info);  
  76.         int rotation = activity.getWindowManager().getDefaultDisplay()  
  77.                 .getRotation();  
  78.         int degrees = 0;  
  79.         Log.d(TAG, "rotation:"+rotation);  
  80.         switch (rotation) {  
  81.             case Surface.ROTATION_0: degrees = 0; break;  
  82.             case Surface.ROTATION_90: degrees = 90; break;  
  83.             case Surface.ROTATION_180: degrees = 180; break;  
  84.             case Surface.ROTATION_270: degrees = 270; break;  
  85.         }  
  86.   
  87.         int result;  
  88.         if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {  
  89.             result = (info.orientation + degrees) % 360;  
  90.             result = (360 - result) % 360;  // compensate the mirror  
  91.         } else {  // back-facing  
  92.             result = (info.orientation - degrees + 360) % 360;  
  93.         }  
  94.         camera.setDisplayOrientation(result);  
  95.     }  
  96.   
  97.     private final class SurfaceViewCallback implements Callback {  
  98.         /** 
  99.          * surfaceView 被创建成功后调用此方法 
  100.          */  
  101.         @Override  
  102.         public void surfaceCreated(SurfaceHolder holder) {  
  103.             Log.d(TAG,"surfaceCreated");  
  104.               
  105.             /*  
  106.              * 在SurfaceView创建好之后 打开摄像头 
  107.              * 注意是 android.hardware.Camera 
  108.              */  
  109. //          2.2获取摄像头number physical cameras   
  110. //          api 9: Camera.getNumberOfCameras()  
  111.             Log.d(TAG, "numbers:"+Camera.getNumberOfCameras());  
  112.             if(Camera.getNumberOfCameras() ==2){  
  113.                   
  114. //              前置摄像头  
  115. //              api 9 :Camera.CameraInfo  
  116.                 camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);  
  117.                 camera.setDisplayOrientation(90);  
  118.             }else{  
  119.                   
  120.                 camera = Camera.open();  
  121.             }  
  122. //          camera = Camera.open();  
  123. //          setCameraDisplayOrientation(CameravedioActivity.this,Camera.CameraInfo.CAMERA_FACING_FRONT,camera);  
  124.   
  125.             Log.d(TAG, "smoothZoom:"+camera.getParameters().isSmoothZoomSupported());  
  126.             Log.d(TAG, "supportzoom:"+camera.getParameters().isZoomSupported());  
  127.             Log.d(TAG, "max zoom:"+camera.getParameters().getMaxZoom());  
  128.             /* 
  129.              * This method must be called before startPreview(). otherwise surfaceview没有图像 
  130.              */  
  131.             try {  
  132.                 camera.setPreviewDisplay(surfaceHolder);  
  133.             } catch (IOException e) {  
  134.                 // TODO Auto-generated catch block  
  135.                 e.printStackTrace();  
  136.             }  
  137.               
  138.             Camera.Parameters parameters = camera.getParameters();  
  139.             /* 每秒从摄像头捕获5帧画面, */  
  140.             parameters.setPreviewFrameRate(5);  
  141.             /* 设置照片的输出格式:jpg */  
  142.             parameters.setPictureFormat(PixelFormat.JPEG);  
  143.             /* 照片质量 */  
  144.             parameters.set("jpeg-quality", 85);  
  145.             mSeekBar.setMax(parameters.getMaxZoom());  
  146.   
  147. //          valid parameters:  
  148.             // 取得相机所支持多少图片大小的个数  
  149.             List<Size> pszize = parameters.getSupportedPreviewSizes();  
  150. //          List<Size> pszize = parameters.getSupportedPictureSizes();  
  151.             if (null != pszize && 0 < pszize.size()) {  
  152.                     int height[] = new int[pszize.size()];// 声明一个数组  
  153.                     Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
  154.                     for (int i = 0; i < pszize.size(); i++) {  
  155.                             Size size = (Size) pszize.get(i);  
  156.                             int sizeheight = size.height;  
  157.                             int sizewidth = size.width;  
  158.                             height[i] = sizeheight;  
  159.                             map.put(sizeheight, sizewidth);  
  160.                             Log.d(TAG, "size.width:"+sizewidth+"\tsize.height:"+sizeheight);  
  161.                     }  
  162.                     Arrays.sort(height);  
  163.                  // 设置  
  164. //                    parameters.setPictureSize(map.get(height[0]),height[0]);  
  165.                     parameters.setPreviewSize(map.get(height[0]),height[0]);            
  166.             }  
  167.   
  168.             /* 将参数对象赋予到 camera 对象上 */  
  169.             camera.setParameters(parameters);  
  170.             camera.startPreview();  
  171.             /** 
  172.              * Installs a callback to be invoked for every preview frame in addition to displaying them on the screen.  
  173.              * The callback will be repeatedly called for as long as preview is active. This method can be called at  
  174.              * any time, even while preview is live. Any other preview callbacks are overridden. 
  175.              * a callback object that receives a copy of each preview frame, or null to stop receiving  
  176.              */  
  177.             camera.setPreviewCallback(new Camera.PreviewCallback(){  
  178.   
  179.                 @Override  
  180.                 public void onPreviewFrame(byte[] data, Camera camera) {  
  181.                     // TODO Auto-generated method stub  
  182.                     //在视频聊天中,这里传送本地frame数据给remote端  
  183. //                  Log.d(TAG, "camera:"+camera);  
  184. //                  Log.d(TAG, "byte:"+data);  
  185.                 }  
  186.                   
  187.             });  
  188.             preview = true;  
  189.         }  
  190.           
  191.         @Override  
  192.         public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  193.                 int height) {  
  194.             Log.d(TAG,"surfaceChanged");  
  195.         }  
  196.         /** 
  197.          * SurfaceView 被销毁时释放掉 摄像头 
  198.          */  
  199.         @Override  
  200.         public void surfaceDestroyed(SurfaceHolder holder) {  
  201.             if(camera != null) {  
  202.                 /* 若摄像头正在工作,先停止它 */  
  203.                 if(preview) {  
  204.                     camera.stopPreview();  
  205.                     preview = false;  
  206.                 }  
  207.                 //如果注册了此回调,在release之前调用,否则release之后还回调,crash  
  208.                 camera.setPreviewCallback(null);  
  209.                 camera.release();  
  210.             }  
  211.         }  
  212.           
  213.     }  
  214.       
  215. }  

注册文件:

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.edu.wtu"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <!-- android:screenOrientation="landscape" 表示横向界面 -->  
  8.         <activity android:name=".CameravedioActivity" android:label="@string/app_name"  android:screenOrientation="portrait">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.     </application>  
  15.     <uses-sdk android:minSdkVersion="7" />  
  16.     <!-- 在SDCard中创建与删除文件权限 -->  
  17.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  18.     <!-- 往SDCard写入数据权限 -->  
  19.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  20.     <!-- 申请使用摄像头的权限 -->  
  21.     <uses-permission android:name="android.permission.CAMERA"/>  
  22. </manifest>  


布局文件:

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5. >  
  6.     <LinearLayout  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:orientation="vertical"  
  10.     >  
  11.         <SurfaceView   
  12.             android:id="@+id/surfaceView"  
  13.             android:layout_width="200dip"   
  14.             android:layout_height="150dip"  
  15.             android:layout_gravity="center"   
  16.             android:visibility="visible"  
  17.         />  
  18.         <LinearLayout  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.         >  
  22.             <SeekBar  
  23.                 android:id="@+id/seekbar"  
  24.                 android:layout_width="fill_parent"  
  25.                 android:layout_height="wrap_content"  
  26.                 android:layout_marginLeft="40dip"  
  27.                 android:layout_marginRight="40dip"  
  28.                 android:layout_gravity="center"  
  29.                 android:progressDrawable="@android:color/transparent"  
  30.                 android:background="@drawable/progressbar_box"  
  31.             />  
  32.         </LinearLayout>  
  33.     </LinearLayout>  
  34. </FrameLayout>  

seekbar用来实现调焦的

Android Camera(二)

标签:

原文地址:http://www.cnblogs.com/qingchen1984/p/4800996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!