欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年12月30日 11:21:38   作者:路邊橋涼  
這篇文章主要介紹了Android拍照得到全尺寸圖片并進行壓縮 的相關(guān)資料,需要的朋友可以參考下

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

<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();
    // 該 值設(shè)為true那么將不返回實際的bitmap,也不給其分配內(nèi)存空間這樣就避免內(nèi)存溢出了。但是允許我們查詢圖片的信息這其中就包括圖片大小信息
    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;
    // 設(shè)置恰當?shù)膇nSampleSize可以使BitmapFactory分配更少的空間以消除該錯誤
    bmOptions.inSampleSize = scaleFactor;
    // 如果inPurgeable設(shè)為True的話表示使用BitmapFactory創(chuàng)建的Bitmap,用于存儲Pixel的內(nèi)存空間在系統(tǒng)內(nèi)存不足時可以被回收
    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拍照得到全尺寸圖片并進行壓縮 的全部內(nèi)容,希望大家喜歡。

相關(guān)文章

  • Android自定義視圖中圖片的處理

    Android自定義視圖中圖片的處理

    Android系統(tǒng)提供了ImageView顯示普通的靜態(tài)圖片,也提供了AnimationDrawable來開發(fā)逐幀動畫,還可通過Animation對普通圖片使用補間動畫。圖形、圖像處理不僅對Android系統(tǒng)的應(yīng)用界面非常重要,而且Android系統(tǒng)上的益智類游戲、2D游戲都需要大量的圖形、圖像處理
    2022-07-07
  • Android編程自定義圓角半透明Dialog的方法

    Android編程自定義圓角半透明Dialog的方法

    這篇文章主要介紹了Android編程自定義圓角半透明Dialog的方法,涉及Android控件的布局及相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下
    2017-03-03
  • Android編程開發(fā)從零開始編寫一個輕量級瀏覽器

    Android編程開發(fā)從零開始編寫一個輕量級瀏覽器

    這篇文章主要為大家介紹了Android編程開發(fā)從零開始編寫一個輕量級瀏覽器過程步驟示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步
    2022-02-02
  • Jetpack?Compose實現(xiàn)對角線滾動效果

    Jetpack?Compose實現(xiàn)對角線滾動效果

    這篇文章主要為大家詳細介紹了如何利用Jetpack?Compose實現(xiàn)一個簡單的對角線滾動效果,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-02-02
  • Android數(shù)據(jù)共享 sharedPreferences 的使用方法

    Android數(shù)據(jù)共享 sharedPreferences 的使用方法

    這篇文章主要介紹了Android數(shù)據(jù)共享 sharedPreferences 的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解使用sharedpreferences,需要的朋友可以參考下
    2017-10-10
  • Kotlin掛起函數(shù)原理示例剖析

    Kotlin掛起函數(shù)原理示例剖析

    這篇文章主要為大家介紹了Kotlin掛起函數(shù)的原理示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Android實現(xiàn)拍照添加時間水印

    Android實現(xiàn)拍照添加時間水印

    這篇文章主要為大家詳細介紹了Android實現(xiàn)拍照添加時間水印,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android使用Pull解析器解析xml文件的實現(xiàn)代碼

    Android使用Pull解析器解析xml文件的實現(xiàn)代碼

    Android使用Pull解析器解析xml文件的實現(xiàn)代碼,需要的朋友可以參考一下
    2013-02-02
  • Android Studio設(shè)置、改變字體和主題的方法

    Android Studio設(shè)置、改變字體和主題的方法

    這篇文章主要介紹了Android Studio設(shè)置、改變字體和主題的方法,需要的朋友可以參考下
    2018-03-03
  • Android最簡單的狀態(tài)切換布局實現(xiàn)教程

    Android最簡單的狀態(tài)切換布局實現(xiàn)教程

    這篇文章主要給大家介紹了關(guān)于Android中最簡單的狀態(tài)切換布局的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2018-10-10

最新評論