欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-17 20:05:46
废话不多说了,直接给大家贴代码了,具体代码如下所示:

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

<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拍照得到全尺寸图片并进行压缩 的全部内容,希望大家喜欢。

上一篇:

下一篇: