二次采样,三级缓存

1.创建一个Util类,里面封装了二次采样和三级缓存的方法

public class ImageUtil {

//二次采样
public static Bitmap TwoImage(String filepath,int width,int heigh){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filepath,options);
    options.inSampleSize = Math.max(options.outHeight / heigh, options.outWidth / width);

    //第二次加载图片
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
    return bitmap;
}

//质量压缩
public static Bitmap conpress(Bitmap bitmap, int qua, File output){
    Bitmap result = bitmap;

    try {
        bitmap.compress(Bitmap.CompressFormat.JPEG,qua,new FileOutputStream(output));
        result = BitmapFactory.decodeFile(output.getAbsolutePath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return result;
}
}

2.在MainActivity里进行调用

public class MainActivity extends AppCompatActivity {

private EditText e1,e2,e3;
private Button b1,b2;
private ImageView img;
private Uri uri;
private final int SREQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取资源ID
    e1 = findViewById(R.id.e1);
    e2 = findViewById(R.id.e2);
    e3 = findViewById(R.id.e3);

    b1 = findViewById(R.id.img1_get);
    b2 = findViewById(R.id.b2);
    img = findViewById(R.id.img);

    //点击按钮进行打开相册
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent,SREQUEST);
        }
    });

    //点击按钮生成图片
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //得到文件路径
            String filePath = getUrl(uri);
            //二次采样
            Bitmap bitmap = ImageUtil.TwoImage(filePath,Integer.parseInt(e1.getText().toString()),Integer.parseInt(e2.getText().toString()));
            bitmap = ImageUtil.conpress(bitmap,Integer.parseInt(e3.getText().toString()),new File(Environment.getExternalStorageDirectory(),"tmp.jpg"));
            img.setImageBitmap(bitmap);
        }
    });
}

//得到文件路径
private String getUrl(Uri path){
    String[] pro = {MediaStore.Images.Media.DATA};
    CursorLoader cursorLoader = new CursorLoader(this, path, pro, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    int columnIndexOrThrow = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(columnIndexOrThrow);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if(requestCode == SREQUEST){
        if(resultCode == RESULT_OK) {
            uri = data.getData();
        }
        return;
    }
}
}

3.布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<Button
    android:id="@+id/img1_get"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="选择图片"
    />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/t1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="宽"/>
    <EditText
        android:id="@+id/e1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/t2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="高"/>
    <EditText
        android:id="@+id/e2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/t3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="压缩质量"/>
    <EditText
        android:id="@+id/e3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />
</LinearLayout>

<Button
    android:id="@+id/b2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="生成图片"/>


<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_43587850/article/details/83860078