二次取樣,三級快取
阿新 • • 發佈:2018-11-30
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>