android 调用相机拍照 并缩放切割图片

本文介绍了一种在Android应用中处理图片的方法,通过调用系统的图片裁剪功能,实现了从相册选择图片或拍照后进行缩放和裁剪的效果。具体包括启动相机、选择相册图片、图片裁剪等功能。
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 
Java代码   收藏代码
  1. package cn.m15.test;    
  2.     
  3. import java.io.ByteArrayOutputStream;    
  4. import java.io.File;    
  5. import android.app.Activity;    
  6. import android.content.Intent;    
  7. import android.graphics.Bitmap;    
  8. import android.net.Uri;    
  9. import android.os.Bundle;    
  10. import android.os.Environment;    
  11. import android.provider.MediaStore;    
  12. import android.view.View;    
  13. import android.view.View.OnClickListener;    
  14. import android.widget.Button;    
  15. import android.widget.ImageView;    
  16.     
  17. public class testActivity extends Activity {    
  18.     
  19.     public static final int NONE = 0;    
  20.     public static final int PHOTOHRAPH = 1;// 拍照    
  21.     public static final int PHOTOZOOM = 2// 缩放    
  22.     public static final int PHOTORESOULT = 3;// 结果    
  23.     
  24.     public static final String IMAGE_UNSPECIFIED = "image/*";    
  25.     ImageView imageView = null;    
  26.     Button button0 = null;    
  27.     Button button1 = null;    
  28.     
  29.     @Override    
  30.     public void onCreate(Bundle savedInstanceState) {    
  31.         super.onCreate(savedInstanceState);    
  32.         setContentView(R.layout.main);    
  33.         imageView = (ImageView) findViewById(R.id.imageID);    
  34.         button0 = (Button) findViewById(R.id.btn_01);    
  35.         button1 = (Button) findViewById(R.id.btn_02);    
  36.     
  37.         button0.setOnClickListener(new OnClickListener() {    
  38.             @Override    
  39.             public void onClick(View v) {    
  40.                 Intent intent = new Intent(Intent.ACTION_PICK, null);    
  41.                 intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);    
  42.                 startActivityForResult(intent, PHOTOZOOM);    
  43.             }    
  44.         });    
  45.     
  46.         button1.setOnClickListener(new OnClickListener() {    
  47.     
  48.             @Override    
  49.             public void onClick(View v) {    
  50.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
  51.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));    
  52.                 startActivityForResult(intent, PHOTOHRAPH);    
  53.             }    
  54.         });    
  55.     }    
  56.     
  57.     @Override    
  58.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
  59.         if (resultCode == NONE)    
  60.             return;    
  61.         // 拍照    
  62.         if (requestCode == PHOTOHRAPH) {    
  63.             //设置文件保存路径这里放在跟目录下    
  64.             File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");    
  65.             startPhotoZoom(Uri.fromFile(picture));    
  66.         }    
  67.             
  68.         if (data == null)    
  69.             return;    
  70.             
  71.         // 读取相册缩放图片    
  72.         if (requestCode == PHOTOZOOM) {    
  73.             startPhotoZoom(data.getData());    
  74.         }    
  75.         // 处理结果    
  76.         if (requestCode == PHOTORESOULT) {    
  77.             Bundle extras = data.getExtras();    
  78.             if (extras != null) {    
  79.                 Bitmap photo = extras.getParcelable("data");    
  80.                 ByteArrayOutputStream stream = new ByteArrayOutputStream();    
  81.                 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件    
  82.                 imageView.setImageBitmap(photo);    
  83.             }    
  84.     
  85.         }    
  86.     
  87.         super.onActivityResult(requestCode, resultCode, data);    
  88.     }    
  89.     
  90.     public void startPhotoZoom(Uri uri) {    
  91.         Intent intent = new Intent("com.android.camera.action.CROP");    
  92.         intent.setDataAndType(uri, IMAGE_UNSPECIFIED);    
  93.         intent.putExtra("crop""true");    
  94.         // aspectX aspectY 是宽高的比例    
  95.         intent.putExtra("aspectX"1);    
  96.         intent.putExtra("aspectY"1);    
  97.         // outputX outputY 是裁剪图片宽高    
  98.         intent.putExtra("outputX"64);    
  99.         intent.putExtra("outputY"64);    
  100.         intent.putExtra("return-data"true);    
  101.         startActivityForResult(intent, PHOTORESOULT);    
  102.     }    
  103. }    


Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical" android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent">    
  5.     <TextView android:layout_width="fill_parent"    
  6.         android:layout_height="wrap_content" android:text="@string/hello" />    
  7.     <ImageView android:id="@+id/imageID"    
  8.         android:adjustViewBounds="true" android:maxWidth="50dip"    
  9.         android:maxHeight="50dip" android:layout_width="wrap_content"    
  10.         android:layout_height="wrap_content" />    
  11.     <Button android:id="@+id/btn_01" android:layout_height="50dip"    
  12.             android:text="相册" android:layout_width="150dip"/>    
  13.     <Button android:id="@+id/btn_02" android:layout_height="50dip"    
  14.             android:text="拍照" android:layout_width="150dip"/>    
  15. </LinearLayout>    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值