本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~

    有些场景中,须要照相并且上传到服务,但是由于图片的巨细太大,那么就

    上传就

    会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就须要减小图片的巨细。减少图片的巨细有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这类情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的巨细,并且满意需求(图片仍就清晰)。上面组要是分析图片的压缩:

    1. 照相请查看    想要保存图片到制定目录,启动Camera应用时,须要指定文件

    final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options);

    2.2 处理图片旋转

   

    int degree = readPictureDegree(filePath); bm = rotateBitmap(bm,degree) ;

    private static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; }


    每日一道理

如果说友谊是一颗常青树,那么,浇灌它的必定是出自心田的清泉;如果说友谊是一朵开不败的鲜花,那么,照耀它的必定是从心中升起的太阳。 多少笑声都是友谊唤起的,多少眼泪都是友谊揩干的。友谊的港湾温情脉脉,友谊的清风灌满征帆。友谊不是感情的投资,它不须要股息和分红。(友谊可以换其他词语)


    private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){ if(bitmap == null) return null ; int w = bitmap.getWidth(); int h = bitmap.getHeight(); // Setting post rotate to 90 Matrix mtx = new Matrix(); mtx.postRotate(rotate); return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); }

    2.3压缩图片

        

    bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表现压缩70%; 如果不压缩是100,表现压缩率为0

    public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options); if(bm == null){ return null; } int degree = readPictureDegree(filePath); bm = rotateBitmap(bm,degree) ; ByteArrayOutputStream baos = null ; try{ baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 30, baos); }finally{ try { if(baos != null) baos.close() ; } catch (IOException e) { e.printStackTrace(); } } return bm ; }

    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio; } return inSampleSize; }

文章结束给大家分享下程序员的一些笑话语录: 3G普不普及现在已经不是看终端了,而是看应用,有好的,便宜实用的应用,花1000多买个能用的智能手机应该不是什么难事。反过来说,你200元拿一个智能手机,没有好的应用,看个电影要几十元,也是没人用3G。

--------------------------------- 原创文章 By 图片和压缩 ---------------------------------