Android調(diào)用手機(jī)拍照功能的方法
本文實(shí)例講述了Android調(diào)用手機(jī)拍照功能的方法。分享給大家供大家參考。具體如下:
一、main.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"> <ImageView android:id="@+id/imageView" android:adjustViewBounds="true" android:layout_gravity="center" android:minWidth="150dip" android:minHeight="150dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btnPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="相冊(cè)" /> <Button android:id="@+id/btnTakePicture" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="拍照" /> </LinearLayout>
二、核心代碼:
package com.ljq.test;
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.widget.Button;
import android.widget.ImageView;
public class TestActivity extends Activity {
private static final int NONE = 0;
private static final int PHOTO_GRAPH = 1;// 拍照
private static final int PHOTO_ZOOM = 2; // 縮放
private static final int PHOTO_RESOULT = 3;// 結(jié)果
private static final String IMAGE_UNSPECIFIED = "image/*";
private ImageView imageView = null;
private Button btnPhone = null;
private Button btnTakePicture = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
btnPhone = (Button) findViewById(R.id.btnPhone);
btnPhone.setOnClickListener(onClickListener);
btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
btnTakePicture.setOnClickListener(onClickListener);
}
private final View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v==btnPhone){ //從相冊(cè)獲取圖片
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, PHOTO_ZOOM);
}else if(v==btnTakePicture){ //從拍照獲取圖片
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
.getExternalStorageDirectory(),"temp.jpg")));
startActivityForResult(intent, PHOTO_GRAPH);
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
// 拍照
if (requestCode == PHOTO_GRAPH) {
// 設(shè)置文件保存路徑
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
if (data == null)
return;
// 讀取相冊(cè)縮放圖片
if (requestCode == PHOTO_ZOOM) {
startPhotoZoom(data.getData());
}
// 處理結(jié)果
if (requestCode == PHOTO_RESOULT) {
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)壓縮文件
//此處可以把Bitmap保存到sd卡中
imageView.setImageBitmap(photo); //把圖片顯示在ImageView控件上
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 收縮圖片
*
* @param uri
*/
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", 300);
intent.putExtra("outputY", 500);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_RESOULT);
}
}
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android啟動(dòng)相機(jī)拍照并返回圖片
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- android 拍照和上傳的實(shí)現(xiàn)代碼
- Android拍照得到全尺寸圖片并進(jìn)行壓縮
- android系統(tǒng)在靜音模式下關(guān)閉camera拍照聲音的方法
- Android實(shí)現(xiàn)調(diào)用系統(tǒng)相冊(cè)和拍照的Demo示例
- Android手機(jī)拍照或選取圖庫圖片作為頭像
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android中使用Camera類編寫手機(jī)拍照App的實(shí)例教程
- Android設(shè)置拍照或者上傳本地圖片的示例
- Android開發(fā)實(shí)現(xiàn)拍照功能的方法實(shí)例解析
相關(guān)文章
Android?模擬地圖定位功能的實(shí)現(xiàn)
這篇文章主要介紹了Android?模擬地圖定位功能的實(shí)現(xiàn),本工程利用手機(jī)自帶的"模擬位置"功能實(shí)現(xiàn)運(yùn)行時(shí)修改LocationManager結(jié)果,需要的朋友可以參考一下2022-02-02
android 引導(dǎo)界面的實(shí)現(xiàn)方法
現(xiàn)在越來越多程序都有引導(dǎo)頁面了。網(wǎng)上資料不全?,F(xiàn)在自己實(shí)現(xiàn)下。2013-06-06
flutter?Bloc?實(shí)現(xiàn)原理示例解析
這篇文章主要為大家介紹了flutter?Bloc實(shí)現(xiàn)原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法(Http?Server)
這篇文章主要介紹了Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法,包括資源的配置,資源的下載和存儲(chǔ),版本的管理,如何根據(jù)實(shí)際url獲取對(duì)應(yīng)HttpServer?bind的url等,需要的朋友可以參考下2022-05-05
Android自定義View實(shí)現(xiàn)支付寶支付成功-極速get花式Path炫酷動(dòng)畫
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)支付寶支付成功-極速get花式Path炫酷動(dòng)畫的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android中控制和禁止ScrollView自動(dòng)滑動(dòng)到底部的方法
這篇文章主要給大家介紹了關(guān)于Android中控制和禁止ScrollView自動(dòng)滑動(dòng)到底部的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android實(shí)現(xiàn)用文字生成圖片的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)用文字生成圖片的示例代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08
Android Studio 恢復(fù)小窗口??磕J?Docked Mode)
這篇文章主要介紹了Android Studio 恢復(fù)小窗口??磕J?Docked Mode),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04

