Android菜鸟笔记-获取摄像头像素值

前言:

获取摄像头的像素值,通过获取相机设置中支持拍照的最大宽度和最大高度就可算到摄像头的像素值了,当然跟手机厂商标称的还是有差别的。比如标称200W的通过计算1200*1600=1920000=192W≈200W。

定义CameraUtils类:

import java.util.List;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Size;

public class CameraUtils {
	
	public static final int CAMERA_FACING_BACK = 0;
	public static final int CAMERA_FACING_FRONT = 1;
	public static final int CAMERA_NONE = 2;
	
	 public static int HasBackCamera()
	  {
		  int numberOfCameras = Camera.getNumberOfCameras();  
	        CameraInfo cameraInfo = new CameraInfo();  
	        for (int i = 0; i < numberOfCameras; i++) {  
	            Camera.getCameraInfo(i, cameraInfo);  
	            if (cameraInfo.facing == CAMERA_FACING_BACK) {  
	                return i;  
	            }  
	        }  
	        return 2;
	  }

	  public static int HasFrontCamera()
	  {
		  int numberOfCameras = Camera.getNumberOfCameras();  
	        CameraInfo cameraInfo = new CameraInfo();  
	        for (int i = 0; i < numberOfCameras; i++) {  
	            Camera.getCameraInfo(i, cameraInfo);  
	            if (cameraInfo.facing == CAMERA_FACING_FRONT) {  
	                return i;  
	            }  
	        }  
	        return 2;
	  }

	  public static String getCameraPixels(int paramInt)
	  {
	    if (paramInt == 2)
	      return "无";
	    Camera localCamera = Camera.open(paramInt);
	    Camera.Parameters localParameters = localCamera.getParameters();
	    localParameters.set("camera-id", 1);
	    List<Size> localList = localParameters.getSupportedPictureSizes();
	    if (localList != null)
	    {
	    	int heights[] = new int[localList.size()]; 
	    	int widths[] = new int[localList.size()]; 
	    	for (int i = 0; i < localList.size(); i++) 
	    	{ 
	    	Size size = (Size) localList.get(i); 
	    	int sizehieght = size.height; 
	    	int sizewidth = size.width; 
	    	heights[i] = sizehieght; 
	    	widths[i] =sizewidth;	 
	    	}
	    	int pixels = getMaxNumber(heights) * getMaxNumber(widths);
		    localCamera.release();
		    return String.valueOf(pixels / 10000) + " 万";
	   
	    }
	    else return "无";
	   
	  }

	  public static int getMaxNumber(int[] paramArray)
	  {
		  int temp = paramArray[0];
		  for(int i = 0;i<paramArray.length;i++)
		  {
			  if(temp < paramArray[i])
			  {
				  temp = paramArray[i];
			  }
		  }
		  return temp;
	  }
}

调用:

String piexls = "\n前置摄像头: "+CameraUtils.getCameraPixels(CameraUtils.HasFrontCamera())
+"\n后置摄像头: "+CameraUtils.getCameraPixels(CameraUtils.HasBackCamera());

OK,Done~

你可能感兴趣的:(android,像素,摄像头)