Android:处理调用系统相机照片被压缩问题

来源:互联网 发布:淘宝介绍范文图片模板 编辑:程序博客网 时间:2024/05/11 17:41

最近使用Intent调用系统的拍照功能,并用onActivityResult方法中的data得到照片的bitmap,但是发现获取的照片资源是被压缩过的,而且被压缩的很小,那么如何得到未被压缩的原图片并按照自己的需要进行压缩呢?

在网上找了一些方法,那就是在跳转时添加intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, uri); 这个属性,这种方式的过程就是将拍摄的图片存储到uri这个路径中,而onActivityResult只是负责显示这个照片,也就是说是提前确定存储的路径。下面上代码

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="java">import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.Matrix;  
  11. import android.net.Uri;  
  12. import android.os.Bundle;  
  13. import android.os.Environment;  
  14. import android.provider.MediaStore;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.ImageView;  
  19.   
  20. public class MainActivity extends Activity {  
  21.     private Button mButton;  
  22.     private ImageView mImage;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         mButton = (Button) findViewById(R.id.button1);  
  29.         mImage = (ImageView) findViewById(R.id.image_show);  
  30.         mButton.setOnClickListener(new OnClickListener() {  
  31.               
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 // 跳转至拍照界面  
  35.                 Intent intentPhote = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  36.                 intentPhote.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);  
  37.                 File out = new File(getPhotopath());  
  38.                 Uri uri = Uri.fromFile(out);  
  39.                 // 获取拍照后未压缩的原图片,并保存在uri路径中  
  40.                 intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, uri);   
  41. //                intentPhote.putExtra(MediaStore.Images.Media.ORIENTATION, 180);  
  42.                 startActivityForResult(intentPhote, 2000);  
  43.             }  
  44.         });  
  45.     }  
  46.     /** 
  47.      * 获取原图片存储路径 
  48.      * @return 
  49.      */  
  50.     private String getPhotopath() {  
  51.         // 照片全路径  
  52.         String fileName = "";  
  53.         // 文件夹路径  
  54.         String pathUrl = Environment.getExternalStorageDirectory()+"/mymy/";  
  55.         String imageName = "imageTest.jpg";  
  56.         File file = new File(pathUrl);  
  57.         file.mkdirs();// 创建文件夹  
  58.         fileName = pathUrl + imageName;  
  59.         return fileName;  
  60.     }  
  61.       
  62.     @Override  
  63.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  64.         super.onActivityResult(requestCode, resultCode, data);  
  65.         if(requestCode == 2000 && resultCode == Activity.RESULT_OK) {  
  66.             Bitmap bitmap = getBitmapFromUrl(getPhotopath(), 313.5462.0);  
  67.             saveScalePhoto(bitmap);  
  68.             mImage.setImageBitmap(bitmap);  
  69.         }  
  70.     }  
  71.     /** 
  72.      * 根据路径获取图片资源(已缩放) 
  73.      * @param url 图片存储路径 
  74.      * @param width 缩放的宽度 
  75.      * @param height 缩放的高度 
  76.      * @return 
  77.      */  
  78.     private Bitmap getBitmapFromUrl(String url, double width, double height) {  
  79.         BitmapFactory.Options options = new BitmapFactory.Options();  
  80.         options.inJustDecodeBounds = true// 设置了此属性一定要记得将值设置为false  
  81.         Bitmap bitmap = BitmapFactory.decodeFile(url);  
  82.         // 防止OOM发生  
  83.         options.inJustDecodeBounds = false;  
  84.         int mWidth = bitmap.getWidth();  
  85.         int mHeight = bitmap.getHeight();  
  86.         Matrix matrix = new Matrix();  
  87.         float scaleWidth = 1;  
  88.         float scaleHeight = 1;  
  89. //        try {  
  90. //            ExifInterface exif = new ExifInterface(url);  
  91. //            String model = exif.getAttribute(ExifInterface.TAG_ORIENTATION);  
  92. //        } catch (IOException e) {  
  93. //            e.printStackTrace();  
  94. //        }  
  95.         // 按照固定宽高进行缩放  
  96.         // 这里希望知道照片是横屏拍摄还是竖屏拍摄  
  97.         // 因为两种方式宽高不同,缩放效果就会不同  
  98.         // 这里用了比较笨的方式  
  99.         if(mWidth <= mHeight) {  
  100.             scaleWidth = (float) (width/mWidth);  
  101.             scaleHeight = (float) (height/mHeight);  
  102.         } else {  
  103.             scaleWidth = (float) (height/mWidth);  
  104.             scaleHeight = (float) (width/mHeight);  
  105.         }  
  106. //        matrix.postRotate(90); /* 翻转90度 */  
  107.         // 按照固定大小对图片进行缩放  
  108.         matrix.postScale(scaleWidth, scaleHeight);  
  109.         Bitmap newBitmap = Bitmap.createBitmap(bitmap, 00, mWidth, mHeight, matrix, true);  
  110.         // 用完了记得回收  
  111.         bitmap.recycle();  
  112.         return newBitmap;  
  113.     }  
  114.       
  115.     /** 
  116.      * 存储缩放的图片 
  117.      * @param data 图片数据 
  118.      */  
  119.     private void saveScalePhoto(Bitmap bitmap) {  
  120.         // 照片全路径  
  121.         String fileName = "";  
  122.         // 文件夹路径  
  123.         String pathUrl = Environment.getExternalStorageDirectory().getPath()+"/mymy/";  
  124.         String imageName = "imageScale.jpg";  
  125.         FileOutputStream fos = null;  
  126.         File file = new File(pathUrl);  
  127.         file.mkdirs();// 创建文件夹  
  128.         fileName = pathUrl + imageName;  
  129.         try {  
  130.             fos = new FileOutputStream(fileName);  
  131.             bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);  
  132.         } catch (FileNotFoundException e) {  
  133.             e.printStackTrace();  
  134.         } finally {  
  135.             try {  
  136.                 fos.flush();  
  137.                 fos.close();  
  138.             } catch (IOException e) {  
  139.                 e.printStackTrace();  
  140.             }  
  141.         }  
  142.     }  
  143.   
  144. }  

布局比较简单

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/button1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="拍照" />  
  11.       
  12.     <ImageView   
  13.         android:id="@+id/image_show"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"/>  
  16.   
  17. </LinearLayout>  

最后别忘了添加权限

<uses-permission Android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


这样就能按照自己的需要存储特定大小的照片了

0 0
原创粉丝点击