二次采样图片

Mantivity主页面:
package com.bw.ymy.ymy1107;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.CursorLoader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
private EditText width,heigh,edit3;//宽 高 质量
private Button but1,but2;
private ImageView image1;
private final int PINK_IMAGE=100;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
width=findViewById(R.id.edit1);
heigh=findViewById(R.id.edit2);
edit3=findViewById(R.id.edit3);
but1=findViewById(R.id.but1);
but2=findViewById(R.id.but2);
image1=findViewById(R.id.image1);

    //获取图片
    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(Intent.ACTION_PICK);
           intent.setType("image/*");
            startActivityForResult(intent,100);

        }
    });
    //生成  二次采样
    but2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                String  filepath=getpath(uri);
        

                //二次采样
            Log.i("TEST",filepath);

           Bitmap bitmap=ImageUtill.scaleBitmap(filepath,Integer.parseInt(width.getText().toString()),Integer.parseInt(heigh.getText().toString()));
           bitmap=ImageUtill.compress(bitmap,Integer.parseInt(edit3.getText().toString()),new File(Environment.getExternalStorageDirectory(),"image.jpg"));
           image1.setImageBitmap(bitmap);
        }
    });



}
private String getpath(Uri contenturi)
{
   String[] proj={MediaStore.Images.Media.DATA};
    CursorLoader loader=new CursorLoader(this,contenturi,proj,null,null,null);
    Cursor cursor=loader.loadInBackground();
    int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return  cursor.getString(column_index);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if(requestCode==100)
    {
        if(resultCode==RESULT_OK)
        {
            //图片
            uri=data.getData();
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

//工具类
package com.bw.ymy.ymy1107;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ImageUtill {

//图片二次采样
public static Bitmap scaleBitmap(String filrpatah,int width,int heigh)
{
    //缩小图片大小
    BitmapFactory.Options  opts=new BitmapFactory.Options();
    opts.inJustDecodeBounds=true;
    BitmapFactory.decodeFile(filrpatah ,opts);
    //匡高
    //opts.width
    //opts.heigh
    opts.inSampleSize=Math.max(opts.outHeight/heigh,opts.outWidth/width);
    //二次加载:真正的加载图片
    opts.inJustDecodeBounds=false;
    Bitmap bitmap=BitmapFactory.decodeFile(filrpatah,opts);
    return  bitmap;
}
//压缩
public static  Bitmap compress(Bitmap bitmap, int qual, File output)
{
    Bitmap result=bitmap;
    try {
        bitmap.compress(Bitmap.CompressFormat.JPEG,qual,new FileOutputStream(output));
        result=BitmapFactory.decodeFile(output.getAbsolutePath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    };
    return  result;
}

}
//布局

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/but1"
    android:text="选择图片"/>
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit1"
    android:hint="请输入宽"

    android:layout_below="@id/but1"

        />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit2"
    android:hint="请输入高"
    android:layout_below="@id/but1"
   android:layout_marginLeft="150dp"

    />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit3"
    android:hint="质量"
    android:layout_below="@id/but1"
    android:layout_marginLeft="300dp"
    />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/but2"
    android:layout_below="@id/edit3"
    android:text="生成"/>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/image1"
    android:background="@drawable/ic_launcher_background"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="100dp"
    android:layout_below="@id/but2"/>
//

猜你喜欢

转载自blog.csdn.net/qq_42902175/article/details/83818895