Android 调用相册 拍照 实现系统控件缩放 切割图片 .

yinbaoshiguang 2011-10-31

转载:http://blog.csdn.net/xys289187120/article/details/6575830

android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家 嘿嘿。

Android 调用相册 拍照 实现系统控件缩放 切割图片 .Android 调用相册 拍照 实现系统控件缩放 切割图片 .Android 调用相册 拍照 实现系统控件缩放 切割图片 .

 package cn.m15.test;

import java.io.ByteArrayOutputStream;

importjava.io.File;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.graphics.Bitmap;

importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.os.Environment;

importandroid.provider.MediaStore;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

import android.widget.ImageView;

public class testActivity extends Activity {

 public static final int NONE = 0;

publicstaticfinalintPHOTOHRAPH=1;//拍照

publicstaticfinalintPHOTOZOOM=2;//缩放

 public static final int PHOTORESOULT = 3;// 结果

 public static final String IMAGE_UNSPECIFIED = "image/*";

ImageViewimageView=null;

Buttonbutton0=null;

 Button button1 = null;

 @Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

imageView=(ImageView)findViewById(R.id.imageID);

button0=(Button)findViewById(R.id.btn_01);

  button1 = (Button) findViewById(R.id.btn_02);

  button0.setOnClickListener(new OnClickListener() {

@Override

publicvoidonClick(Viewv){

Intentintent=newIntent(Intent.ACTION_PICK,null);

intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);

startActivityForResult(intent,PHOTOZOOM);

}

  });

  button1.setOnClickListener(new OnClickListener() {

   @Override

publicvoidonClick(Viewv){

Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(newFile(Environment.getExternalStorageDirectory(),"temp.jpg")));

startActivityForResult(intent,PHOTOHRAPH);

}

});

 }

 @Override

protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){

if(resultCode==NONE)

return;

//拍照

if(requestCode==PHOTOHRAPH){

//设置文件保存路径这里放在跟目录下

Filepicture=newFile(Environment.getExternalStorageDirectory()+"/temp.jpg");

startPhotoZoom(Uri.fromFile(picture));

}

if(data==null)

return;

//读取相册缩放图片

if(requestCode==PHOTOZOOM){

startPhotoZoom(data.getData());

}

//处理结果

if(requestCode==PHOTORESOULT){

Bundleextras=data.getExtras();

if(extras!=null){

Bitmapphoto=extras.getParcelable("data");

ByteArrayOutputStreamstream=newByteArrayOutputStream();

photo.compress(Bitmap.CompressFormat.JPEG,75,stream);//(0-100)压缩文件

imageView.setImageBitmap(photo);

   }

  }

  super.onActivityResult(requestCode, resultCode, data); }

 public void startPhotoZoom(Uri uri) {

Intentintent=newIntent("com.android.camera.action.CROP");

intent.setDataAndType(uri,IMAGE_UNSPECIFIED);

intent.putExtra("crop","true");

//aspectXaspectY是宽高的比例

intent.putExtra("aspectX",1);

intent.putExtra("aspectY",1);

//outputXoutputY是裁剪图片宽高

intent.putExtra("outputX",64);

intent.putExtra("outputY",64);

intent.putExtra("return-data",true);

startActivityForResult(intent,PHOTORESOULT);

}

}

<?xml version="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextViewandroid:layout_width="fill_parent"

android:layout_height="wrap_content"android:text="@string/hello"/>

<ImageViewandroid:id="@+id/imageID"

android:adjustViewBounds="true"android:maxwidth="50dip"

android:maxheight="50dip"android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<Buttonandroid:id="@+id/btn_01"android:layout_height="50dip"

android:text="相册"android:layout_width="150dip"/>

<Buttonandroid:id="@+id/btn_02"android:layout_height="50dip"

android:text="拍照"android:layout_width="150dip"/>

</LinearLayout>

相关推荐