android——拍照,相册图片剪切其实就这么简单

时间:2024-12-26 13:03:38

接触android这么久了。还没有真正的浩浩看看android拍照,相册图片剪切到底是怎么回事,每次都是从别人的代码一扣,就过来了。其实,谷歌提供的API已经很强大。只需要用的好,就那么几句就可以搞定神秘的拍照和剪切图片。废话不多少,直接看源码

package com.example.testcamera;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener {
private Button btnc, btnp;
private ImageView img;
private Intent intent;
private final int dataFromCamera = 1;
private final int dataFromAlbum = 2;
private final int cropFromCamera = 3;
private final int cropFromAlbum = 4;
private Uri mUri = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
btnc = (Button) findViewById(R.id.btnc);
btnp = (Button) findViewById(R.id.btnp);
img = (ImageView) findViewById(R.id.img);
btnc.setOnClickListener(this);
btnp.setOnClickListener(this); } @Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnc:
openCamera();
break;
case R.id.btnp:
openPhoto();
break;
default:
break;
} } private void openCamera() {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, dataFromCamera);
} private void openPhoto() {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, dataFromAlbum);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri uri = null;
if (data.getData() != null) {
uri = data.getData();
mUri = uri;
}
switch (requestCode) {
case dataFromCamera:
cropImageUri(uri, 800, 600, cropFromCamera);
break;
case dataFromAlbum:
cropImageUri(uri, 800, 600, cropFromAlbum);
break;
case cropFromCamera:
if (mUri != null) {
Bitmap bitmap = decodeUriAsBitmap(mUri);
img.setImageBitmap(bitmap);
}
break;
case cropFromAlbum:
if (mUri != null) {
Bitmap bitmap = decodeUriAsBitmap(mUri);
img.setImageBitmap(bitmap);
}
break;
default:
break; }
} } private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 4); intent.putExtra("aspectY", 3); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); } private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
} }

xml布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btnc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_purple"
android:text="拍照"
android:textColor="@android:color/white" /> <Button
android:id="@+id/btnp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_dark"
android:text="从相册中选取"
android:textColor="@android:color/white" /> <ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp" /> </LinearLayout>

mainfest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testcamera"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

注意要配置权限呀。