Android编程实现图片拍照剪裁的方法
本文实例讲述了Android实现图片拍照剪裁的方法。分享给大家供大家参考,具体如下:
调用系统的裁剪工具对相册或者拍照的图片进行裁剪。
startActivityforResult用的很恰当,一些系统action需要注意。
packagecom.photosizing; importjava.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; importandroid.widget.ImageView; publicclasstestActivityextendsActivity{ publicstaticfinalintNONE=0; publicstaticfinalintPHOTOHRAPH=1;//拍照 publicstaticfinalintPHOTOZOOM=2;//缩放 publicstaticfinalintPHOTORESOULT=3;//结果 publicstaticfinalStringIMAGE_UNSPECIFIED="image/*"; ImageViewimageView=null; Buttonbutton0=null; Buttonbutton1=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(newOnClickListener(){ @Override publicvoidonClick(Viewv){ Intentintent=newIntent(Intent.ACTION_PICK,null); intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult(intent,PHOTOZOOM); } }); button1.setOnClickListener(newOnClickListener(){ @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(data==null) return; //拍照 if(requestCode==PHOTOHRAPH){ //设置文件保存路径这里放在跟目录下 Filepicture=newFile(Environment.getExternalStorageDirectory() +"/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); } //读取相册缩放图片 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); } publicvoidstartPhotoZoom(Uriuri){ 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文件:
<?xmlversion="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>
希望本文所述对大家Android程序设计有所帮助。