歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android調用系統攝像頭拍照並剪裁壓縮

Android調用系統攝像頭拍照並剪裁壓縮

日期:2017/3/1 9:35:56   编辑:Linux編程

由於業務需要寫了一個Android手機拍照的功能Demo,同時實現了圖片剪裁和圖片壓縮。以下是源碼

package com.klp.demo_025;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

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.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView iv;
private Button button;
private File file;
private Uri uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1);

file = new File(this.getExternalFilesDir(null), "image.jpg");
uri = Uri.fromFile(file);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 2);
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
startPhotoZoom(uri);
} else {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(),
options);
// 壓縮圖片
// bitmap = compressImage(bitmap,500);

if (bitmap != null) {
// 顯示圖片
iv.setImageBitmap(bitmap);
// 保存圖片
FileOutputStream fos = null;
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
} catch (Exception e) {
// TODO: handle exception

}
}

}

}

/**
* 裁剪圖片
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");// crop=true 有這句才能出來最後的裁剪頁面.
intent.putExtra("aspectX", 1);// 這兩項為裁剪框的比例.
intent.putExtra("aspectY", 1);// x:y=1:1
intent.putExtra("outputX", 200);//圖片輸出大小
intent.putExtra("outputY", 200);
intent.putExtra("output", uri);
intent.putExtra("outputFormat", "JPEG");// 返回格式
startActivityForResult(intent, 3);
}

/**
* 將圖片image壓縮成大小為 size的圖片(size表示圖片大小,單位是KB)
*
* @param image
* 圖片資源
* @param size
* 圖片大小
* @return Bitmap
*/
private Bitmap compressImage(Bitmap image, int size) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 質量壓縮方法,這裡100表示不壓縮,把壓縮後的數據存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
// 循環判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
while (baos.toByteArray().length / 1024 > size) {
// 重置baos即清空baos
baos.reset();
// 每次都減少10
options -= 10;
// 這裡壓縮options%,把壓縮後的數據存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, options, baos);

}
// 把壓縮後的數據baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
// 把ByteArrayInputStream數據生成圖片
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}

}

下面是布局,很簡單

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:src="@drawable/ic_launcher" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="拍照" />

</RelativeLayout>

最後注意,一定要在AndroidManifest.xml添加SD卡訪問權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

最簡單的Ubuntu Touch & Android 雙系統安裝方式 http://www.linuxidc.com/Linux/2014-01/94881.htm

在Nexus上實現Ubuntu和Android 4.4.2 雙啟動 http://www.linuxidc.com/Linux/2014-05/101849.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-04/101148.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved