Android編程實現(xiàn)圖片拍照剪裁的方法
本文實例講述了Android實現(xiàn)圖片拍照剪裁的方法。分享給大家供大家參考,具體如下:
調(diào)用系統(tǒng)的裁剪工具對相冊或者拍照的圖片進行裁剪。
startActivityforResult用的很恰當,一些系統(tǒng)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;// 結(jié)果
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) {
// 設(shè)置文件保存路徑這里放在跟目錄下
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
// 讀取相冊縮放圖片
if (requestCode == PHOTOZOOM) {
startPhotoZoom(data.getData());
}
// 處理結(jié)果
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程序設(shè)計有所幫助。
- Android啟動相機拍照并返回圖片
- Android實現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android仿微信發(fā)表說說實現(xiàn)拍照、多圖上傳功能
- android 拍照和上傳的實現(xiàn)代碼
- Android拍照得到全尺寸圖片并進行壓縮
- android系統(tǒng)在靜音模式下關(guān)閉camera拍照聲音的方法
- Android手機拍照或選取圖庫圖片作為頭像
- Android中使用Camera類編寫手機拍照App的實例教程
- Android拍照或從圖庫選擇圖片并裁剪
- Android拍照裁剪圖片
- Android仿微信選擇圖片和拍照功能
- Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件示例【附demo源碼下載】
相關(guān)文章
Android編程使用內(nèi)容提供者方式(ContentProvider)進行存儲的方法
這篇文章主要介紹了Android編程使用內(nèi)容提供者方式進行存儲的方法,涉及Android內(nèi)容提供者的創(chuàng)建,配置及針對數(shù)據(jù)的增刪改查等操作技巧,需要的朋友可以參考下2016-01-01
Android OkHttp實現(xiàn)全局過期token自動刷新示例
本篇文章主要介紹了Android OkHttp實現(xiàn)全局過期token自動刷新示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Android數(shù)據(jù)庫greenDAO配置與使用介紹
這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫,使用數(shù)據(jù)庫存儲時候,一般都會使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫的方法,需要的朋友可以參考下2023-03-03
基于Android10渲染Surface的創(chuàng)建過程
這篇文章主要介紹了基于Android10渲染Surface的創(chuàng)建過程,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
Android ProgressBar 模擬進度條效果的實現(xiàn)
這篇文章主要介紹了Android ProgressBar 模擬進度條效果的實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
android 字體顏色選擇器(ColorPicker)介紹
本文將詳細介紹android 字體顏色選擇器(ColorPicker)需要了解更多的朋友可以參考下2012-11-11
利用OPENCV為android開發(fā)畸變校正的JNI庫方法
今天小編就為大家分享一篇利用OPENCV為android開發(fā)畸變校正的JNI庫方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

