安卓通过camera的API进行变焦

最近在开发相机类的APP,需要用到Camera的API,一般的参数(如分辨率,对焦区域等等)都是直接设置比较一个容易理解的参数就可以了。然鹅,在变化焦距的时候遇到了问题,设置焦距的方法是调用Camera.Parameters的setZoom(int value)方法,但是仔细一看,value是个整形啊,变焦为1.5倍的时候该怎么办????(下面是我瞎JB扯的内容,你们可以看也可以不看,想快速解决问题就直接拉到底就OJBK了)

流下了没技术的泪水

翻遍了几乎整个百度(本人不会用谷歌搜索),还是没找到完整的解决方法。关于zoom的方法主要有几个(isSmoothZoomSupported,setZoom,getZoom,getMaxZoom,getZoomRatios);只有getZoomRatios这个方法的返回值是一个集合,但是看了大半天还是没看出是个啥集合。。。。下面就是我的华为手机打印出来的集合。。。。

[100, 103, 106, 109, 112, 115, 118, 121, 124, 127, 130, 133, 136, 139, 142, 145, 148, 151, 154, 157, 160, 163, 166, 169, 172, 175, 178, 181, 184, 187, 190, 193, 196, 199, 203, 206, 209, 212, 215, 218, 221, 224, 227, 230, 233, 236, 239, 242, 245, 248, 251, 254, 257, 260, 263, 266, 269, 272, 275, 278, 281, 284, 287, 290, 293, 296, 299, 303, 306, 309, 312, 315, 318, 321, 324, 327, 330, 333, 336, 339, 342, 345, 348, 351, 354, 357, 360, 363, 366, 369, 372, 375, 378, 381, 384, 387, 390, 393, 396, 399]

看得我一脸萌币,,,不过,我仔细一看,集合的数量是100,跟getMaxZoom返回的99貌似有出入,想了一下,0-99不就是100个数吗!!!!!!!好像有了大突破,然后再查看了资料,原来集合的内容就是实际变焦的倍数*100。。。。。。到这里,事情就好解决了,只要我输入个倍数获取这个集合的下标i,再把i作为参数传入setZoom方法就行了。


好啦好啦,相关代码在下面:

    /**
     * 获取下标,传入的参数为浮点数,就是显示的倍率
     *
     * @param zoomratio
     * @return
     */
    public int getZoomIndexByZoomratio(float zoomratio) {
        List<Integer> allZoomRatios = getAllZoomRatio();   
        if (allZoomRatios == null) {
            showErrorMessage("获取zoom集合失败");
            return -3;
        } else if (allZoomRatios.size() <= 0) {
            showErrorMessage("获取zoom集合为空");
            return -4;
        }
        if (zoomratio == 1.0f) {
            return 0;
        }
        if (zoomratio == getPictureMaxZoom()) {
            return allZoomRatios.size() - 1;
        }
        for (int i = 1; i < allZoomRatios.size(); i++) {
            if (allZoomRatios.get(i) >= (zoomratio * 100) && allZoomRatios.get(i - 1) <= (zoomratio * 100)) {
                return i;
            }
        }

        return -1;
    }
    /**
     * 获取全部zoomratio
     */
    public List<Integer> getAllZoomRatio() {
        Camera.Parameters parameters = getCameraParameters();
        if (parameters.isZoomSupported()) {
            return parameters.getZoomRatios();
        } else {
            return null;
        }
    }
    /**
     * 获取实际意义的最大放大倍数,如4.0,10.0
     * 未完成
     *
     * @return
     */
    public float getPictureMaxZoom() {
        List<Integer> allZoomRatio = getAllZoomRatio();
        if (null == allZoomRatio) {
            return 1.0f;
        } else {
            return Math.round(allZoomRatio.get(allZoomRatio.size() - 1) / 100f);
        }
    }
    /**
     * 获取全部zoomratio
     */
    public List<Integer> getAllZoomRatio() {
        Camera.Parameters parameters = getCameraParameters();
        if (parameters.isZoomSupported()) {
            Log.e("lawwing", "getAllZoomRation = " + parameters.getZoomRatios().toString());
            return parameters.getZoomRatios();
        } else {
            return null;
        }
    }

最后提醒一句,不是所有手机都支持变焦的,所以调用相关接口的时候要进行isZoomSupported进行判断。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • public classCamera.Parametersextends Objectjava.lang.Obje...
    冉冉升起的小太阳阅读 10,988评论 0 7
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,135评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,890评论 18 139
  • 普及单反相机镜头知识,限摄影初学者、爱好者。都是一些理论知识。 以下内容来源于网络,如有侵权请务必私信告知。 镜头...
    何日君再来阅读 5,268评论 4 72
  • 喔,打出来才发现今天真是数字不错的一天。就是心情不是特别嗨皮~ 微信朋友圈,QQ空间,微博,都溜了一圈,居然,也不...
    西东东_4950阅读 206评论 0 0