Coder Social home page Coder Social logo

clipheadicon's Introduction

ClipHeadIcon

微信头像剪裁Demo

效果如下

网上搜到了以下参考资料,其中clip-image由于项目太繁琐我没有采用,转而看了clip-image作者引用的2个csdn链接

https://github.com/msdx/clip-image http://blog.csdn.net/lmj623565791/article/details/39761281 http://blog.csdn.net/xiechengfa/article/details/45702427

最后一个链接中的代码其实已经是很完善了,但是有一个问题, 就是当图片过大的时候处理速度过慢,然后传递过程会出现问题, 比如小米4拍摄的图片分辨率很大,头像其实没必要过于清晰,设置头像大小和手机屏幕差不多宽就可以满足需求了,所以我将代码修改了

ClipImageActivity中的代码片段:

//不在内存中读取图片的宽高
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
//注意此处为了解决1080p手机拍摄图片过大所以做了一定压缩,否则bitmap在小米4以及拍图比较大的机型上会显示黑屏
opts.inSampleSize = width > 1080 ? (int)(width / 1080) : 1 ;
opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true

并且将图片剪裁的时候设置了大小控制,比如我们是500kb的显示那就是

ClipZoomImageView文件中的代码片段:

//将剪裁的图片压缩到500k以下,如果没需求就注释该段代码

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       int options = 100;//保存的图片自动压缩低于500k
       bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);  
       while (baos.toByteArray().length / 1024 > 500) {   
           baos.reset();  
           options -= 10;  
           bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);  
   } 

如果你所需要的上传图片是其他值直接将500改成你所要的就可以了

具体分析请看:http://www.hloong.com/?p=328

clipheadicon's People

Contributors

hloong avatar

Watchers

codeever avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.