最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android拍照得到全尺寸圖片并進(jìn)行壓縮

 更新時(shí)間:2015年12月30日 11:21:38   作者:路邊橋涼  
這篇文章主要介紹了Android拍照得到全尺寸圖片并進(jìn)行壓縮 的相關(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);
    // 意圖 相機(jī)
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
    // 如果有相機(jī)
    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那么將不返回實(shí)際的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è)置恰當(dāng)?shù)膇nSampleSize可以使BitmapFactory分配更少的空間以消除該錯(cuò)誤
    bmOptions.inSampleSize = scaleFactor;
    // 如果inPurgeable設(shè)為True的話表示使用BitmapFactory創(chuàng)建的Bitmap,用于存儲(chǔ)Pixel的內(nèi)存空間在系統(tǒng)內(nèi)存不足時(shí)可以被回收
    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拍照得到全尺寸圖片并進(jìn)行壓縮 的全部?jī)?nèi)容,希望大家喜歡。

相關(guān)文章

最新評(píng)論

互助| 崇仁县| 江口县| 虹口区| 泗水县| 大庆市| 灵宝市| 文化| 上思县| 苏尼特右旗| 博爱县| 徐汇区| 名山县| 奈曼旗| 开封市| 麻栗坡县| 分宜县| 马边| 饶河县| 左贡县| 新余市| 渝北区| 武威市| 长乐市| 乡城县| 青铜峡市| 安丘市| 贡觉县| 旬邑县| 宜兰县| 佳木斯市| 东至县| 资源县| 通山县| 建水县| 盐山县| 东乌| 建昌县| 繁峙县| 和田县| 甘洛县|