图片的质量压缩和二次采样

二者的不同点

  1. 质量压缩:只会减少图片在sdcard中占的内存大小而不会改变运行内存不改变图片本身像素
  2. 二次采样:改变图片本身像素,减少加载图片的本身运行内存
    上代码:
    代码中注释都比较详细
package com.example.administrator.zhiliangyasuoandbendihuancun;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import java.io.FileOutputStream;

import static android.graphics.BitmapFactory.decodeFile;

public class MainActivity extends AppCompatActivity {
    private ImageView image1,image2;
    private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image1= (ImageView) findViewById(R.id.img1);
        image2= (ImageView) findViewById(R.id.img2);
        getData();
        getDtata02();
    }
//    质量压缩减少本地的使用内存但是不会改变所占的运行内存
    public  void getData(){
        String url="/mnt/sdcard/aa.jpg";
//        使用Bitmap来操作图片首先使用Bitmap来加载一张图片

         bitmap= BitmapFactory.decodeFile(url);
       int width= bitmap.getWidth();
       int height= bitmap.getHeight();
        int count=bitmap.getRowBytes()*bitmap.getHeight();
       int count1= bitmap.getByteCount();
        Log.e("image", "onCreate: "+width+"---"+height+"---"+count+"---"+count1);
        image1.setImageBitmap(bitmap);
//        将得到的图片进行质量压缩然后在进行比较
//        这个就是讲图片质量压缩后保存
        try {
            FileOutputStream str = new FileOutputStream("/mnt/sdcard/aa_60.jpg");
            bitmap.compress(Bitmap.CompressFormat.JPEG,60,str);
             width= bitmap.getWidth();
            height= bitmap.getHeight();
             count=bitmap.getRowBytes()*bitmap.getHeight();
             count1= bitmap.getByteCount();
            Log.e("压缩后的比较", "onCreate: "+width+"---"+height+"---"+count+"---"+count1);
//            把它也适配一下比较图片之间的差距
//            其实图片上基本上没有差距但是占的存储内存少了好多
            bitmap= decodeFile("/mnt/sdcard/aa_60.jpg");
            image2.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    二次采样
    public  void  getDtata02(){
//第一次采样只是把边框加载进来而不是具体的像素点
                BitmapFactory.Options options=new BitmapFactory.Options();
//        设置只加载边框的属性为true
        options.inJustDecodeBounds=true;
      bitmap=  BitmapFactory.decodeFile("/mnt/sdcard/aa.jpg",options);
        image1.setImageBitmap(bitmap);
//        获取原图的宽和高
      int h=  options.outHeight;
        int w=options.outWidth;
//        定义缩放比例
        int Size=1;
//        如果高或者宽没有没有缩放到控件定义的大小就继续缩放
//        我在listview控件中定义的宽和高是300和200
        while (h/Size>200||w/Size>300){
                    Size*=2;//如果没有缩放到达到的要求继续缩放

        }
        /**
         * 二次采样
         */
//        二次采样需要把像素点都加载出来
        options.inJustDecodeBounds=false;
//        通过上面的循环就可以得到具体Size设置的大小
        options.inSampleSize=Size;
//        设置图片的格式
            options.inPreferredConfig=Bitmap.Config.ARGB_8888;
       bitmap= BitmapFactory.decodeFile("/mnt/sdcard/aa.jpg",options);
        image2.setImageBitmap(bitmap);
    }
}

布局里面


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.zhiliangyasuoandbendihuancun.MainActivity">
<LinearLayout
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/img1"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/img2"/>


LinearLayout>

RelativeLayout>

质量布局和二次采样的布局是复用的所以直接覆盖了

最后配置读取sdcard的权限

 <uses-permission android:name="android.permission.INTERNET"/>

这里写图片描述
以上是对质量压缩后图片像素的比较发现值是相同的,并且一般压缩不低于30不会影响视觉效果。

你可能感兴趣的:(安卓)