美文网首页
使用Android系统相机捕获图片(四)

使用Android系统相机捕获图片(四)

作者: R_flash | 来源:发表于2018-11-13 15:55 被阅读0次

    写文章的目的

    1. 静下心来学习
    2. 知识点的积累
    3. 总结,做笔记

    导读

    解决上一篇留下的问题:

    获取了这么大的图片并显示,内存肯定消耗很大,有什么优化吗?

    需求

    调用系统相机捕获图片,并合理显示图片

    代码解构

    • 调用系统相机捕获图片前面几篇已解决,此处不表。
    • 合理显示图片:主要是内存上优化。

    1.回顾一下,上一篇中我们是如何加载拍照后的图片:直接通过BitmapFactory去解码图片路径。

    Bitmap bitmap = BitmapFactory.decodeFile(mImageFilePath);
    Log.i("--bitmap1--", bitmap1.getHeight() + "<===>" + bitmap1.getWidth());
    //I/--bitmap1--: 4608<===>3456
    imageView.setImageBitmap(bitmap);
    

    2.快速加载大图,获取采样后的图片。decodeFile()还有一个重载方法,多一个参数BitmapFactory.Options,通过Options.inSampleSize的值去设置采样比例。比如:Options.inSampleSize = 8表示获取后的bitmap宽高均缩小了8倍,面积缩小了8*8倍。

                //1.快速加载大图,获取采样后的图片 1/8比例 对比打印:其实是宽高都缩小8倍
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                Bitmap bitmap2 = BitmapFactory.decodeFile(mImageFilePath, options);
                Log.i("--bitmap2--", bitmap2.getHeight() + "<===>" + bitmap2.getWidth());
                //I/--bitmap2--: 576<===>432
                imageView.setImageBitmap(bitmap2);
    

    3.了解了简单的采样方法后,接下来我们按照具体环境配置比例,尽可能地填充显示范围:比如显示屏宽高。

    大部分应该是此种需求,需要去计算采样的比例。这个时候我们需要Options的另一个属性inJustDecodeBounds:如果设置为true,解码器会返回null,不会返回bitmap,但是BitmapFactory.Options.outxxx属性会被设置值,运行解码器去查询bitmap,不会将bitmap加载进内存。

               //2.按照具体环境配置比例,尽可能地填充显示范围:比如显示屏宽高
                //接收屏幕宽高 x==width;y==height
                Point disPlayer = new Point();
                //获取屏幕宽高
                getWindowManager().getDefaultDisplay().getSize(disPlayer);
                //创建解码图像的Options
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                //inJustDecodeBounds如果设置为true,解码器会返回null,不会返回bitmap,
                // 但是BitmapFactory.Options.outxxx属性会被设置值,运行解码器去查询bitmap,不会将bitmap加载进内存。
                bitmapOptions.inJustDecodeBounds = true;
                //让解码器去查询该图片(返回bitmap为null,但是给BitmapFactory.Options.outxxx属性设置值)
                BitmapFactory.decodeFile(mImageFilePath, bitmapOptions);
                //显示屏宽高与图片宽高对比
                //屏幕高 disPlayer.y
                //屏幕宽disPlayer.x
                //图片高 bitmapOptions.outHeight
                //图片宽 bitmapOptions.outWidth
                //高之比
                int heightRatio = (int) Math.ceil(bitmapOptions.outHeight / ((float) disPlayer.y));
                //宽之比
                int widthRatio = (int) Math.ceil(bitmapOptions.outWidth / ((float) disPlayer.x));
                //解释下Math.round(),Math.ceil(),Math.floor()
                //1,Math.round():round是附近的意思,取四舍五入
                //2,Math.ceil():ceil是天花板的意思,取上限值
                //3,Math.floor():floor是地板的意思,取下限值
                //比如我打印的屏幕高: 2034;图片高: 4608;比值大概是2.+,ceil之后的值就是3。
                Log.i("--屏幕高 ", disPlayer.y + "");
                Log.i("--屏幕宽 ", disPlayer.x + "");
                Log.i("--图片高 ", bitmapOptions.outHeight + "");
                Log.i("--图片宽 ", bitmapOptions.outWidth + "");
                Log.i("--高之比", heightRatio + "");
                Log.i("--宽之比", widthRatio + "");
                //I/--屏幕高: 2034
                //I/--屏幕宽: 1080
                //I/--图片高: 4608
                //I/--图片宽: 3456
                //I/--高之比: 3
                //I/--宽之比: 4
                if (heightRatio > 1 && widthRatio > 1) {
                    //两个比例都大于1
                    //采样的比例取heightRatio和widthRatio中的最大值
                    bitmapOptions.inSampleSize = Math.max(heightRatio, widthRatio);
                }
                //设置为false取解码图片
                bitmapOptions.inJustDecodeBounds = false;
                //获取缩小之后的图片
                Bitmap bitmap = BitmapFactory.decodeFile(mImageFilePath, bitmapOptions);
                imageView.setImageBitmap(bitmap);
                Log.i("--缩小后宽:",bitmap.getWidth()+"");
                Log.i("--缩小后高:",bitmap.getHeight()+"");
                //I/--缩小后宽:: 864
                //I/--缩小后高:: 1152
    

    4.结果显示。(根据屏幕宽高采样.jpg)(直接缩小8倍采样.jpg)


    根据屏幕宽高采样
    直接缩小8倍采样

    总结

    • 该篇文章解决的问题:减少显示图片时的内存消耗
    • 两个重载函数BitmapFactory.decodeFile(String pathName, Options opts)BitmapFactory.decodeFile(String pathName)
    • Options中两个重要的属性inSampleSize采样比例和inJustDecodeBounds

    代码样例

    1.源码地址
    2.在上一篇的代码基础上修改onActivityResult()即可。

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode != RESULT_OK) {
                return;
            }
            if (requestCode == IMAGE_RESULT) {
                //这里不能通过Intent对象去获取"data",
                // 因为在打开相机时已经通过MediaStore.EXTRA_OUTPUT告诉相机:你把图片放在我传给你的Uri中
                //所以可以直接通过BitmapFactory在存储路径中获取图片
    
                /*Bitmap bitmap1 = BitmapFactory.decodeFile(mImageFilePath);
                Log.i("--bitmap1--", bitmap1.getHeight() + "<===>" + bitmap1.getWidth());
                //I/--bitmap1--: 4608<===>3456*/
    
               /* //1.快速加载大图,获取采样后的图片 1/8比例 对比打印:其实是宽高都缩小8倍
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                Bitmap bitmap2 = BitmapFactory.decodeFile(mImageFilePath, options);
                Log.i("--bitmap2--", bitmap2.getHeight() + "<===>" + bitmap2.getWidth());
                //I/--bitmap2--: 576<===>432
                imageView.setImageBitmap(bitmap2);*/
    
                //2.按照具体环境配置比例,尽可能地填充显示范围:比如显示屏宽高
                //接收屏幕宽高 x==width;y==height
                Point disPlayer = new Point();
                //获取屏幕宽高
                getWindowManager().getDefaultDisplay().getSize(disPlayer);
                //创建解码图像的Options
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                //inJustDecodeBounds如果设置为true,解码器会返回null,不会返回bitmap,
                // 但是BitmapFactory.Options.outxxx属性会被设置值,运行解码器去查询bitmap,不会将bitmap加载进内存。
                bitmapOptions.inJustDecodeBounds = true;
                //让解码器去查询该图片(返回bitmap为null,但是给BitmapFactory.Options.outxxx属性设置值)
                BitmapFactory.decodeFile(mImageFilePath, bitmapOptions);
                //显示屏宽高与图片宽高对比
                //屏幕高 disPlayer.y
                //屏幕宽disPlayer.x
                //图片高 bitmapOptions.outHeight
                //图片宽 bitmapOptions.outWidth
                //高之比
                int heightRatio = (int) Math.ceil(bitmapOptions.outHeight / ((float) disPlayer.y));
                //宽之比
                int widthRatio = (int) Math.ceil(bitmapOptions.outWidth / ((float) disPlayer.x));
                //解释下Math.round(),Math.ceil(),Math.floor()
                //1,Math.round():round是附近的意思,取四舍五入
                //2,Math.ceil():ceil是天花板的意思,取上限值
                //3,Math.floor():floor是地板的意思,取下限值
                //比如我打印的屏幕高: 2034;图片高: 4608;比值大概是2.+,ceil之后的值就是3。
                Log.i("--屏幕高 ", disPlayer.y + "");
                Log.i("--屏幕宽 ", disPlayer.x + "");
                Log.i("--图片高 ", bitmapOptions.outHeight + "");
                Log.i("--图片宽 ", bitmapOptions.outWidth + "");
                Log.i("--高之比", heightRatio + "");
                Log.i("--宽之比", widthRatio + "");
                //I/--屏幕高: 2034
                //I/--屏幕宽: 1080
                //I/--图片高: 4608
                //I/--图片宽: 3456
                //I/--高之比: 3
                //I/--宽之比: 4
                if (heightRatio > 1 && widthRatio > 1) {
                    //两个比例都大于1
                    //采样的比例取heightRatio和widthRatio中的最大值
                    bitmapOptions.inSampleSize = Math.max(heightRatio, widthRatio);
                }
                //设置为false取解码图片
                bitmapOptions.inJustDecodeBounds = false;
                //获取缩小之后的图片
                Bitmap bitmap = BitmapFactory.decodeFile(mImageFilePath, bitmapOptions);
                imageView.setImageBitmap(bitmap);
                Log.i("--缩小后宽:",bitmap.getWidth()+"");
                Log.i("--缩小后高:",bitmap.getHeight()+"");
                //I/--缩小后宽:: 864
                //I/--缩小后高:: 1152
            }
        }
    

    相关文章

      网友评论

          本文标题:使用Android系统相机捕获图片(四)

          本文链接:https://www.haomeiwen.com/subject/xihoxqtx.html