Android 圖片的讀取 縮放 保存

jopen 11年前發布 | 25K 次閱讀 Android Android開發 移動開發

注意AndroidManifest配置權限:(在SD卡寫入數據)

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

代碼:

從另一個Activity 上傳入byte型的數據,通過 bmp = BitmapFactory.decodeByteArray(txInfo, 0, txInfo.length);生成位圖。

然后有用到ZoomControls圖片縮放,最后可以保存圖片到sd卡。

package com.login.main;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ZoomControls;

public class List_tx extends Activity {

 private ImageView imageView1;
 private ZoomControls zoom;
 private Button saveButton;
 private float scaleWidth = 1;
 private float scaleHeight = 1;
 private Bitmap bmp;
 private int zooms = 0;
 private ProgressDialog m_Dialog = null;
 private Handler handler = new Handler();
 private String xh = "";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list_tx);
  Bundle b = this.getIntent().getExtras();
  byte[] txInfo = b.getByteArray("txInfo");
  xh = b.getString("xh");

  saveButton = (Button) findViewById(R.id.saveButton);
  saveButton.setOnClickListener(saveClick);
  imageView1 = (ImageView) findViewById(R.id.imageView1);
  bmp = BitmapFactory.decodeByteArray(txInfo, 0, txInfo.length);// 生成位圖
  imageView1.setImageBitmap(bmp);

  zoom = (ZoomControls) findViewById(R.id.zoomcontrol);
  zoom.setIsZoomInEnabled(true);
  zoom.setIsZoomOutEnabled(true);

  // 圖片放大
  zoom.setOnZoomInClickListener(new OnClickListener() {
   public void onClick(View v) {
    zoom.setIsZoomOutEnabled(true);
    int bmpWidth = bmp.getWidth();
    int bmpHeight = bmp.getHeight();
    // 設置圖片放大但比例
    double scale = 1.25;
    // 計算這次要放大的比例
    if (zooms >= -3 && zooms < 6) {
     scaleWidth = (float) (scaleWidth * scale);
     scaleHeight = (float) (scaleHeight * scale);
     // 產生新的大小但Bitmap對象
     Matrix matrix = new Matrix();
     matrix.postScale(scaleWidth, scaleHeight);
     Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
       bmpHeight, matrix, true);
     imageView1.setImageBitmap(resizeBmp);
     zooms++;
    } else
     zoom.setIsZoomInEnabled(false);

   }

  });
  // 圖片減小
  zoom.setOnZoomOutClickListener(new OnClickListener() {

   public void onClick(View v) {
    zoom.setIsZoomInEnabled(true);
    int bmpWidth = bmp.getWidth();
    int bmpHeight = bmp.getHeight();
    // 設置圖片放大但比例
    double scale = 0.8;
    // 計算這次要放大的比例
    if (zooms > -3 && zooms <= 6) {
     scaleWidth = (float) (scaleWidth * scale);
     scaleHeight = (float) (scaleHeight * scale);
     // 產生新的大小但Bitmap對象
     Matrix matrix = new Matrix();
     matrix.postScale(scaleWidth, scaleHeight);
     Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
       bmpHeight, matrix, true);
     imageView1.setImageBitmap(resizeBmp);
     zooms--;
    } else
     zoom.setIsZoomOutEnabled(false);
   }

  });

 }

 // 保存
 OnClickListener saveClick = new OnClickListener() {

  public void onClick(final View v) {
   m_Dialog = ProgressDialog.show(List_tx.this, "請稍后...", "圖像保存中...",
     true);

   new Thread(new Runnable() {

    public void run() {
     File file = new File("/sdcard/" + xh + ".jpeg");
     try {
      FileOutputStream out = new FileOutputStream(file);
      if (bmp.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
       out.flush();
       out.close();
      }
     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      System.out.println("FileNotFoundException");
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      System.out.println("FileNotFoundException");
      e.printStackTrace();
     }

     // 更新界面
     handler.post(new Runnable() {
      public void run() {
       Toast.makeText(getApplicationContext(),
         "頭像保存在" + "/sdcard/" + xh + ".jpeg",
         Toast.LENGTH_SHORT).show();
      }
     });
     m_Dialog.dismiss();
    }
   }).start();
  }

 };

}

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!