Android:处理调用系统相机照片被压缩问题
来源:互联网 发布:网络布线的工作量 编辑:程序博客网 时间:2024/06/04 15:18
最近使用Intent调用系统的拍照功能,并用onActivityResult方法中的data得到照片的bitmap,但是发现获取的照片资源是被压缩过的,而且被压缩的很小,那么如何得到未被压缩的原图片并按照自己的需要进行压缩呢?
在网上找了一些方法,那就是在跳转时添加intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, uri); 这个属性,这种方式的过程就是将拍摄的图片存储到uri这个路径中,而onActivityResult只是负责显示这个照片,也就是说是提前确定存储的路径。下面上代码
<pre name="code" class="java">import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;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 { private Button mButton; private ImageView mImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.button1); mImage = (ImageView) findViewById(R.id.image_show); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 跳转至拍照界面 Intent intentPhote = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intentPhote.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); File out = new File(getPhotopath()); Uri uri = Uri.fromFile(out); // 获取拍照后未压缩的原图片,并保存在uri路径中 intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, uri); // intentPhote.putExtra(MediaStore.Images.Media.ORIENTATION, 180); startActivityForResult(intentPhote, 2000); } }); } /** * 获取原图片存储路径 * @return */ private String getPhotopath() { // 照片全路径 String fileName = ""; // 文件夹路径 String pathUrl = Environment.getExternalStorageDirectory()+"/mymy/"; String imageName = "imageTest.jpg"; File file = new File(pathUrl); file.mkdirs();// 创建文件夹 fileName = pathUrl + imageName; return fileName; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 2000 && resultCode == Activity.RESULT_OK) { Bitmap bitmap = getBitmapFromUrl(getPhotopath(), 313.5, 462.0); saveScalePhoto(bitmap); mImage.setImageBitmap(bitmap); } } /** * 根据路径获取图片资源(已缩放) * @param url 图片存储路径 * @param width 缩放的宽度 * @param height 缩放的高度 * @return */ private Bitmap getBitmapFromUrl(String url, double width, double height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 设置了此属性一定要记得将值设置为false Bitmap bitmap = BitmapFactory.decodeFile(url); // 防止OOM发生 options.inJustDecodeBounds = false; int mWidth = bitmap.getWidth(); int mHeight = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = 1; float scaleHeight = 1;// try {// ExifInterface exif = new ExifInterface(url);// String model = exif.getAttribute(ExifInterface.TAG_ORIENTATION);// } catch (IOException e) {// e.printStackTrace();// } // 按照固定宽高进行缩放 // 这里希望知道照片是横屏拍摄还是竖屏拍摄 // 因为两种方式宽高不同,缩放效果就会不同 // 这里用了比较笨的方式 if(mWidth <= mHeight) { scaleWidth = (float) (width/mWidth); scaleHeight = (float) (height/mHeight); } else { scaleWidth = (float) (height/mWidth); scaleHeight = (float) (width/mHeight); }// matrix.postRotate(90); /* 翻转90度 */ // 按照固定大小对图片进行缩放 matrix.postScale(scaleWidth, scaleHeight); Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight, matrix, true); // 用完了记得回收 bitmap.recycle(); return newBitmap; } /** * 存储缩放的图片 * @param data 图片数据 */ private void saveScalePhoto(Bitmap bitmap) { // 照片全路径 String fileName = ""; // 文件夹路径 String pathUrl = Environment.getExternalStorageDirectory().getPath()+"/mymy/"; String imageName = "imageScale.jpg"; FileOutputStream fos = null; File file = new File(pathUrl); file.mkdirs();// 创建文件夹 fileName = pathUrl + imageName; try { fos = new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }}布局比较简单
<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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" /> <ImageView android:id="@+id/image_show" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>
最后别忘了添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这样就能按照自己的需要存储特定大小的照片了
2 2
- Android:处理调用系统相机照片被压缩问题
- Android:处理调用系统相机照片被压缩问题
- Android:处理调用系统相机照片被压缩问题,图片内容看不清的问题
- 关于调用系统相机以及压缩照片
- android调用系统相机,获取照片的适配性问题
- android调用系统相机,获取照片的适配性问题
- Android中调用系统相机获取照片的尺寸问题
- Android调用系统相机照片和裁剪
- Android 调用系统相机处理大图片问题
- Android调用系统相机问题
- 调用系统相机导致照片旋转问题的修复
- H5调用相机,裁剪,压缩照片
- Android调用系统相机方法及保存照片方式
- android--调用系统相机拍照,显示并保存照片 - zhengwen
- Android Camera解析(上) 调用系统相机拍摄照片
- android 调用系统相机拍照并保存照片原图
- Android调用系统的相机保证照片的质量
- android调用系统相机并剪切照片(个人学习)
- 使用JNI_OnLoad()函数实现本地方法的注册
- 内核对象(VC_Win32)
- SQL 语句大全
- 数据的标准化
- SQL SERVER 正则匹配实例分享--【叶子】
- Android:处理调用系统相机照片被压缩问题
- PC值=当前程序执行位置+8
- vs2010无法连接到asp.net developer server
- 小团队git开发模式
- 文件的格式与文件扩展名指定的格式不一致的解决办法
- 第一份工作
- Android中TelephonyManager类来获取imei号和其他手机信息
- PHP代码网站如何防范SQL注入漏洞攻击建议
- mysql 日期操作 增减天数、时间转换、时间戳