国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android拍照得到全尺寸圖片并進行壓縮

2020-04-11 11:05:48
字體:
來源:轉載
供稿:網友

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <Button    android:id="@+id/take_photo"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="Take Photo" />  <Button    android:id="@+id/get_photo"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="get Photo" />  <ImageView    android:id="@+id/picture"    android:layout_width="300dp"    android:layout_height="300dp"    android:layout_gravity="center_horizontal" /></LinearLayout>package com.example.choosepictest;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity implements OnClickListener {  static final int REQUEST_IMAGE_CAPTURE = 1;  private Button takePhoto;  private Button getPhoto;  private ImageView picture;  private Uri imgUri;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    takePhoto = (Button) findViewById(R.id.take_photo);    getPhoto = (Button) findViewById(R.id.get_photo);    picture = (ImageView) findViewById(R.id.picture);    takePhoto.setOnClickListener(this);    getPhoto.setOnClickListener(this);  }    @Override  public void onClick(View v) {    switch (v.getId()) {    case R.id.take_photo:      dispatchTakePictureIntent();      break;    default:      break;    }  }  // 保存全尺寸照片  String mCurrentPhotoPath;  private void dispatchTakePictureIntent() {    File appDir = new File(Environment.getExternalStorageDirectory(),        "/etoury/picCache");    if (!appDir.exists()) {      appDir.mkdir();    }    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")        .format(new Date());    String fileName = timeStamp + ".jpg";    File outputImage = new File(appDir, fileName);    try {      if (outputImage.exists()) {        outputImage.delete();      }      outputImage.createNewFile();    } catch (IOException e) {      e.printStackTrace();    }    mCurrentPhotoPath = outputImage.getAbsolutePath();    imgUri = Uri.fromFile(outputImage);    // 意圖 相機    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");    intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);    // 如果有相機    if (intent.resolveActivity(getPackageManager()) != null) {      startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);    }  }  //解碼縮放圖片(Decode a Scaled Image)  private void setPic() {    // Get the dimensions of the View    int targetW = picture.getWidth();    int targetH = picture.getHeight();    // Get the dimensions of the bitmap    BitmapFactory.Options bmOptions = new BitmapFactory.Options();    // 該 值設為true那么將不返回實際的bitmap,也不給其分配內存空間這樣就避免內存溢出了。但是允許我們查詢圖片的信息這其中就包括圖片大小信息    bmOptions.inJustDecodeBounds = true;    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);    int photoW = bmOptions.outWidth;    int photoH = bmOptions.outHeight;    // Determine how much to scale down the image    // Math.min求最小值    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);    // Decode the image file into a Bitmap sized to fill the View    bmOptions.inJustDecodeBounds = false;    // 設置恰當的inSampleSize可以使BitmapFactory分配更少的空間以消除該錯誤    bmOptions.inSampleSize = scaleFactor;    // 如果inPurgeable設為True的話表示使用BitmapFactory創建的Bitmap,用于存儲Pixel的內存空間在系統內存不足時可以被回收    bmOptions.inPurgeable = true;    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);    picture.setImageBitmap(bitmap);  }  @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (resultCode == RESULT_OK) {      switch (requestCode) {      case REQUEST_IMAGE_CAPTURE:        setPic();        break;      default:        break;      }    }  }}

以上代碼就是本文給大家介紹的Android拍照得到全尺寸圖片并進行壓縮 的全部內容,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莒南县| 绥宁县| 白河县| 临江市| 朝阳县| 屏边| 剑河县| 林西县| 桐柏县| 青铜峡市| 泉州市| 武威市| 阿坝| 忻州市| 嘉峪关市| 宝山区| 丹巴县| 乐亭县| 宝鸡市| 枣阳市| 新巴尔虎右旗| 息烽县| 黄大仙区| 曲水县| 高碑店市| 宁乡县| 淮滨县| 诏安县| 濮阳县| 邯郸县| 施秉县| 平安县| 仙桃市| 祁连县| 鹿邑县| 威信县| 北碚区| 林州市| 湖南省| 霍州市| 南华县|