Android编程实现图片拍照剪裁的方法
程序员文章站
2023-12-12 12:29:52
本文实例讲述了android实现图片拍照剪裁的方法。分享给大家供大家参考,具体如下:
调用系统的裁剪工具对相册或者拍照的图片进行裁剪。
startactivityfor...
本文实例讲述了android实现图片拍照剪裁的方法。分享给大家供大家参考,具体如下:
调用系统的裁剪工具对相册或者拍照的图片进行裁剪。
startactivityforresult用的很恰当,一些系统action需要注意。
package com.photosizing; import java.io.bytearrayoutputstream; import java.io.file; import android.app.activity; import android.content.intent; import android.graphics.bitmap; 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 testactivity extends activity { public static final int none = 0; public static final int photohraph = 1;// 拍照 public static final int photozoom = 2; // 缩放 public static final int photoresoult = 3;// 结果 public static final string image_unspecified = "image/*"; imageview imageview = null; button button0 = null; button button1 = null; @override public void oncreate(bundle savedinstancestate) { 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 public void onclick(view v) { intent intent = new intent(intent.action_pick, null); intent.setdataandtype( mediastore.images.media.external_content_uri, image_unspecified); startactivityforresult(intent, photozoom); } }); button1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mediastore.action_image_capture); intent.putextra(mediastore.extra_output, uri.fromfile(new file( environment.getexternalstoragedirectory(), "temp.jpg"))); startactivityforresult(intent, photohraph); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == none) return; if (data == null) return; // 拍照 if (requestcode == photohraph) { // 设置文件保存路径这里放在跟目录下 file picture = new file(environment.getexternalstoragedirectory() + "/temp.jpg"); startphotozoom(uri.fromfile(picture)); } // 读取相册缩放图片 if (requestcode == photozoom) { startphotozoom(data.getdata()); } // 处理结果 if (requestcode == photoresoult) { bundle extras = data.getextras(); if (extras != null) { bitmap photo = extras.getparcelable("data"); bytearrayoutputstream stream = new bytearrayoutputstream(); photo.compress(bitmap.compressformat.jpeg, 75, stream); // (0 - 100)压缩文件 imageview.setimagebitmap(photo); } } super.onactivityresult(requestcode, resultcode, data); } public void startphotozoom(uri uri) { intent intent = new intent("com.android.camera.action.crop"); intent.setdataandtype(uri, image_unspecified); intent.putextra("crop", "true"); // aspectx aspecty 是宽高的比例 intent.putextra("aspectx", 1); intent.putextra("aspecty", 1); // outputx outputy 是裁剪图片宽高 intent.putextra("outputx", 64); intent.putextra("outputy", 64); intent.putextra("return-data", true); startactivityforresult(intent, photoresoult); } }
xml文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <imageview android:id="@+id/imageid" android:adjustviewbounds="true" android:maxwidth="50dip" android:maxheight="50dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/btn_01" android:layout_height="50dip" android:text="相册" android:layout_width="150dip"/> <button android:id="@+id/btn_02" android:layout_height="50dip" android:text="拍照" android:layout_width="150dip"/> </linearlayout>
希望本文所述对大家android程序设计有所帮助。