寫文章的目的
- 靜下心來學(xué)習(xí)
- 知識(shí)點(diǎn)的積累
- 總結(jié),做筆記
導(dǎo)讀
個(gè)人認(rèn)為系統(tǒng)相機(jī)捕獲圖片是一個(gè)復(fù)合功能點(diǎn)钟哥,包括調(diào)用相機(jī)、拍照鄙信、返回圖片瞪醋,還要考慮實(shí)際的應(yīng)用場(chǎng)景:如手機(jī)版本等,因此會(huì)分為幾篇文章去講解装诡,由簡(jiǎn)入繁银受。每一篇主體是示例代碼,通過代碼去理解這個(gè)功能鸦采。
需求
- 調(diào)用系統(tǒng)相機(jī)
- 返回圖片
- 顯示圖片
代碼解構(gòu)
1.調(diào)用系統(tǒng)相機(jī):使用Intent方式宾巍,action
使用的是MediaStore.ACTION_IMAGE_CAPTURE
∮娌看到這么長(zhǎng)的參數(shù)可能會(huì)很慌顶霞,其實(shí)不用著急。MediaStore
是一個(gè)Media provider锣吼,包含內(nèi)部存儲(chǔ)和外部存儲(chǔ)的媒體元數(shù)據(jù)选浑。ACTION_IMAGE_CAPTURE
里面單詞也很簡(jiǎn)單:圖片捕獲。那么整個(gè)其實(shí)也就能理解了:相機(jī)就是用來拍照的玄叠,拍照也就是捕獲圖片古徒,拍完后需要存儲(chǔ)。廢話少說读恃,直接上代碼隧膘。
//創(chuàng)建intent ,設(shè)置action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//調(diào)用相機(jī)
startActivityForResult(intent, IMAGE_RESULT);
2.返回圖片并顯示:既然使用startActivityForResult
寺惫,當(dāng)然要用onActivityResult
去接收疹吃。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK ){
return;
}
if(requestCode == IMAGE_RESULT){
//通過data獲取bundle數(shù)據(jù)
Bundle extras = data.getExtras();
//拍照后系統(tǒng)會(huì)返回一個(gè)bitmap,key是data西雀。(沒有找到常量去代替data)
Bitmap bitmap = (Bitmap) extras.get("data");
//顯示圖片
imageView.setImageBitmap(bitmap);
}
}
3.顯示結(jié)果:會(huì)發(fā)現(xiàn)只顯示了一點(diǎn)點(diǎn)萨驶??艇肴?并不是我的ImageView只用這么大篡撵,而是返回的圖片只用這么一點(diǎn)判莉。可以在獲取打印bitmap 的寬高驗(yàn)證一下育谬。
總結(jié)
那么,使用系統(tǒng)相機(jī)捕獲圖片最簡(jiǎn)單的樣例就講完了帮哈√盘矗可能會(huì)有疑問:這么一點(diǎn)圖片有什么用?圖片太小看的我眼睛疼娘侍,我需要大圖又該怎么實(shí)現(xiàn)咖刃?請(qǐng)聽下回分解。
代碼樣例
1.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="capture_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn" />
</android.support.constraint.ConstraintLayout>
2.java代碼
package com.rflash.captrueimage01;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private ImageView imageView;
private final int IMAGE_RESULT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
imageView = findViewById(R.id.iv);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn:
//打開相機(jī)
openCameraForResult();
break;
}
}
/**
* 打開相機(jī)
*/
private void openCameraForResult() {
//創(chuàng)建intent 憾筏,設(shè)置action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//調(diào)用相機(jī)
startActivityForResult(intent, IMAGE_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == IMAGE_RESULT) {
//通過data獲取bundle數(shù)據(jù)
Bundle extras = data.getExtras();
//拍照后系統(tǒng)會(huì)返回一個(gè)bitmap嚎杨,key是data。(沒有找到常量去代替data)
Bitmap bitmap = (Bitmap) extras.get("data");
//打印bitmap寬高
Log.d("--width", bitmap.getWidth() + "");
Log.d("--height", bitmap.getHeight() + "");
//顯示圖片
imageView.setImageBitmap(bitmap);
}
}
}