服务器之家

服务器之家 > 正文

Android拍照得到全尺寸图片并进行压缩

时间:2021-04-27 15:31     来源/作者:路边桥凉

废话不多说了,直接给大家贴代码了,具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<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拍照得到全尺寸图片并进行压缩 的全部内容,希望大家喜欢。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部