1. 程式人生 > >二次取樣圖片

二次取樣圖片

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"/>
//