假设现在有这样一个需求需要一直开着手机摄像头 但是不做任何拍照动作 

但是每个手机的相机分辨率都不同

而默认预览的时候参数是最大分辨率 这样有时候就回导致电量损耗的加快 

所以我们可以采取降低相机分辨率的方法 这样既达到了目标 又节省了电量

 

  1 package com.example.camerbatterytest;
  2 
  3 
  4 import java.io.IOException;
  5 import java.util.List;
  6 
  7 import android.app.Activity;
  8 import android.graphics.PixelFormat;
  9 import android.hardware.Camera;
 10 import android.hardware.Camera.Size;
 11 import android.os.Bundle;
 12 import android.util.Log;
 13 import android.view.Menu;
 14 import android.view.MenuItem;
 15 import android.view.SurfaceHolder;
 16 import android.view.SurfaceView;
 17 import android.widget.Button;
 18 
 19 public class CamerActivity extends Activity implements SurfaceHolder.Callback{
 20 
 21     Button takephoto;//未实现
 22     private SurfaceView mySurfaceView;
 23     private SurfaceHolder mysurfaceHolder;
 24     Camera camera;//5.0以后这个camera已经不推荐使用
 25     @Override
 26     protected void onCreate(Bundle savedInstanceState) {
 27         super.onCreate(savedInstanceState);
 28         setContentView(R.layout.activity_camer);
 29         takephoto=(Button)findViewById(R.id.takephoto);
 30         mySurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
 31         mysurfaceHolder=mySurfaceView.getHolder();
 32         mysurfaceHolder.addCallback(this);
 33         
 34     }
 35     @Override
 36     public void surfaceCreated(SurfaceHolder holder) {
 37         Log.d("huang","surfaceCreated");  
 38         camera = Camera.open();  
 39 
 40     }
 41     @Override
 42     public void surfaceChanged(SurfaceHolder holder, int format, int width,
 43             int height) {
 44         Log.d("huang","surfaceview width="+width+" and height="+height);
 45         Camera.Parameters parameters = camera.getParameters();
 52         Camera.Size bestSize=getBestPreviewSize(width,height,parameters);
 53         parameters.setPreviewSize(bestSize.width, bestSize.height);
55 camera.setParameters(parameters); 56 57 try { 58 camera.setPreviewDisplay(mysurfaceHolder); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 camera.setDisplayOrientation(90);//显示的方向旋转角度 63 camera.startPreview(); 64 65 } 66 @Override 67 public void surfaceDestroyed(SurfaceHolder holder) 68 { 69 Log.d("huang","surfaceDestroyed"); 70 if (camera != null) 71 { 72 camera.stopPreview(); 73 camera.release(); 74 } 75 76 } 77 78 private Camera.Size getBestPreviewSize(int surfaceViewWidth, int surfaceViewHeight, Camera.Parameters parameters) { 79 Camera.Size bestSize=null; 80         //不同机器 尺寸大小排序方式不一样  有的从小到大有的从大到小 81 for (Camera.Size size : parameters.getSupportedPreviewSizes()) 82 { 83 if (size.width <= surfaceViewWidth && size.height <= surfaceViewHeight) 84 { 85 if (bestSize == null) //初始化一个值 86 bestSize=size; 87 else 88 { 89 int tempArea=bestSize.width * bestSize.height; 90 int newArea=size.width * size.height; 91 92 if (newArea > tempArea) //取满足条件里面最大的 93 bestSize=size; 94 } 95 } 96 } 97 98 return bestSize; 99 } 100 101 102 103 }

 

主要是

getBestPreviewSize()方法获得了最合适的分辨率 即既小于当前surfaceview的width height又取支持分辨率里面最大的

版权声明:本文为sweetculiji原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sweetculiji/p/4488109.html?utm_source=tuicool&utm_medium=referral