安卓(Android) 怎么获取支持的相机(Camera)大小

2019年10月27日22:50:27安卓(Android) 怎么获取支持的相机(Camera)大小已关闭评论 336 1273字阅读4分14秒

1.简介

安卓(Android) 怎么获取支持的相机(Camera)大小?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import android.annotation.TargetApi;
import android.hardware.Camera;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Main{
    private static HashMap<Integer, List<Camera.Size>> camearSizeTable = new HashMap<>(
            2);
    public static List<Camera.Size> getSupportCameraSize(int cameraType) {
        List<Camera.Size> sizes = camearSizeTable.get(cameraType);
        if (sizes == null) {
            Camera camera = CameraHelper.getDefaultCamera(cameraType);
            if (camera != null) {
                try {
                    sizes = camera.getParameters().getSupportedVideoSizes();
                    if (sizes == null) {
                        sizes = camera.getParameters()
                                .getSupportedPreviewSizes();
                    }
                    camearSizeTable.put(cameraType, sizes);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (camera != null)
                camera.release();
        }
        return sizes;
    }
    
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static Camera getDefaultCamera(int position) {
        
        int mNumberOfCameras = Camera.getNumberOfCameras();

        
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < mNumberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == position) {
                try {
                    return Camera.open(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}

 
继续阅读
编程十万个怎么办