iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android拍照得到全尺寸图片并进行压缩
  • 434
分享到

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

压缩图片android拍照Android 2022-06-06 09:06:49 434人浏览 薄情痞子
摘要

废话不多说了,直接给大家贴代码了,具体代码如下所示: <LinearLayout xmlns:Android="Http://schemas.android.com/a

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


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

您可能感兴趣的文章:Java的JNI快速入门教程(推荐)安卓应用开发通过java调用c++ jni的图文使用方法Java进阶:JNI使用技巧点滴基于jni调用时,JVM报错问题的深入分析android图片压缩的3种方法实例Android图片压缩上传之基础篇Android中3种图片压缩处理方法Android图片压缩以及优化实例Android图片压缩几种方式总结JNI方法实现图片压缩(压缩率极高)


--结束END--

本文标题: Android拍照得到全尺寸图片并进行压缩

本文链接: https://www.lsjlt.com/news/26061.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android图片压缩(质量压缩和尺寸压缩)
    在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可...
    99+
    2023-05-31
    android 图片压缩 roi
  • Android 拍照并对照片进行裁剪和压缩实例详解
    Android 拍照并对照片进行裁剪和压缩实例详解本文主要介绍 Android 调用摄像头拍照并对照片进行裁剪和压缩,文中给出了主要步骤和关键代码。调用摄像头拍照,对拍摄照片进行裁剪,代码如下。private void showCamera...
    99+
    2023-05-30
    裁剪 android 拍照
  • H5如何调用相机拍照并压缩图片
    这篇文章给大家分享的是有关H5如何调用相机拍照并压缩图片的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。背景最近要做一个h6的页面,主要功能就是调用相机拍照或者是相册选图并且把照片...
    99+
    2024-04-02
  • Android性能优化之图片大小,尺寸压缩的方法
    这篇文章主要介绍“Android性能优化之图片大小,尺寸压缩的方法”,在日常操作中,相信很多人在Android性能优化之图片大小,尺寸压缩的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android性能...
    99+
    2023-06-30
  • Android性能优化之图片大小,尺寸压缩综合解决方案
    目录前言常见的图片压缩方法质量压缩尺寸压缩libjpeg图片压缩流程总结前言 在Android中我们经常会遇到图片压缩的场景,比如给服务端上传图片,包括个人信息的用户头像,有时候人脸...
    99+
    2024-04-02
  • Android应用中怎么对图片进行压缩
    Android应用中怎么对图片进行压缩?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。为何要压缩体积的原因如果你的图片是要准备上传的,那动辄几M的大小肯定不行的,况且图片分辨率大...
    99+
    2023-05-31
    android roi
  • 如何在Android应用中对图片进行压缩
    本篇文章给大家分享的是有关如何在Android应用中对图片进行压缩,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1、质量压缩法设置bitmap options属性,降低图片的质...
    99+
    2023-05-31
    android 中对 roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作