android系統(tǒng)拍照完整流程

1犀盟、拍照相片存儲(chǔ)的臨時(shí)文件

File savePath=new File(Environment.getExternalStorageDirectory(),"/temp/temp.jpg");

2、開啟系統(tǒng)相機(jī)

Intent cameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri imageUri = Uri.fromFile(savePath);

// 指定照片保存路徑(SD卡),temp.jpg為一個(gè)臨時(shí)文件宏赘,每次拍照后這個(gè)圖片都會(huì)被替換

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(cameraIntent, Constant.PHOTO_GRAPH);

3、當(dāng)拍照完畢后系統(tǒng)會(huì)調(diào)用onActivityResult

@Override

public void onActivityResult(int requestCode,int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode) {

case Constant.PHOTO_GRAPH:

if(RESULT_CANCELED!= resultCode) {

//根據(jù)savePath.getAbsolutePath()絕對(duì)路徑獲取當(dāng)前拍攝的圖片去做相關(guān)邏輯操作

//保存圖片到系統(tǒng)內(nèi)容提供器

url[0] = MediaStore.Images.Media.insertImage(appContext.getContentResolver(),

picPath,null,null);

}

}

break;


4蝌数、創(chuàng)建本地圖片存放路徑

private String createPicPath(String path) {

File dirFile =new File(Environment.getExternalStorageDirectory().getPath()

+ path);

if(!dirFile.exists()) {

dirFile.mkdirs();

}

// 格式化時(shí)間

String dataTake =new SimpleDateFormat(

"yyyyMMddHHmmssSSS").format(newDate());

return dirFile.getPath() + File.separator+ dataTake +".jpg";

}

4艰管、保存本地圖片

public boolean saveImage(String path, String spath) {

BufferedOutputStream bos =null;

try{

bos =new BufferedOutputStream(

new FileOutputStream(spath,false));

Bitmap photo = getBitmap(path);

photo.compress(Bitmap.CompressFormat.JPEG,80, bos);

bos.flush();

}catch(Exception e) {

e.printStackTrace();

return false;

}finally{

try{

if(bos !=null)

bos.close();

}catch(IOException e) {

e.printStackTrace();

return false;

}

}

return true;

}

5、縮放圖片狈醉,壓縮質(zhì)量

public boolean saveImage(String path, String spath) {

BufferedOutputStream bos =null;

try{

bos =new BufferedOutputStream(

newFileOutputStream(spath,false));

Bitmap photo = getBitmap(path);

photo.compress(Bitmap.CompressFormat.JPEG,80, bos);

bos.flush();

}catch(Exception e) {

e.printStackTrace();

return false;

}finally{

try{

if(bos !=null)

bos.close();

}catch(IOException e) {

e.printStackTrace();

return false;

}

}

return true;

}

6廉油、壓縮比例

public Bitmap getBitmap(String filePath) {

final BitmapFactory.Options options =newBitmapFactory.Options();

options.inJustDecodeBounds=true;

BitmapFactory.decodeFile(filePath, options);

options.inSampleSize=calculateInSampleSize1(options,480,800);

options.inJustDecodeBounds=false;

Bitmap bitmap = toRotationBitmap(BitmapFactory.decodeFile(filePath, options), getExifOrientation(filePath));

returnbitmap;

}

7、旋轉(zhuǎn)圖片

private Bitmap toRotationBitmap(Bitmap bitmap,final introtation) {

if(rotation ==0)

return bitmap;

Matrix matrix =new Matrix();

matrix.postRotate(rotation);

int height = bitmap.getHeight();

int width = bitmap.getWidth();

float scale =1f;

// 防止內(nèi)存溢出苗傅,先添加一個(gè)基本判斷抒线,根據(jù)測(cè)試情況更改

while(height >2200) {

height /=2;

width /=2;

scale *=0.5f;

}

matrix.postScale(scale, scale);

int neHeight = height;

int neWidth = width;

if(rotation ==90|| rotation ==270) {

neHeight = width;

neWidth = height;

}

Bitmap canvasBitmap = Bitmap.createBitmap(neWidth, neHeight,

Bitmap.Config.ARGB_8888);

Canvas canvas =newCanvas(canvasBitmap);

float transX = height;

float transY =0;

if(rotation ==90) {

transX = height;

transY =0;

}else if(rotation ==180) {

transX = width;

transY = height;

}else if(rotation ==270) {

transX =0;

transY = width;

}else{

transX =0;

transY =0;

}

matrix.postTranslate(transX, transY);

canvas.drawBitmap(bitmap, matrix,newPaint());

if(bitmap !=null) {

bitmap.recycle();

bitmap =null;

System.gc();

}

return canvasBitmap;

}

8、獲取拍攝角度

public int getExifOrientation(String path) {

String img_path = path;

System.out.println("圖片位置"+ img_path);

int degree =0;

ExifInterface exif =null;

try{

exif =new ExifInterface(img_path);

}catch(IOException e) {

e.printStackTrace();

}

if(exif !=null) {

intorientation = exif.getAttributeInt(

ExifInterface.TAG_ORIENTATION, -1);

if(orientation != -1) {

// We only recognize a subset of orientation tag values.

switch(orientation) {

case ExifInterface.ORIENTATION_ROTATE_90:

degree =90;

break;

case ExifInterface.ORIENTATION_ROTATE_180:

degree =180;

break;

case ExifInterface.ORIENTATION_ROTATE_270:

degree =270;

break;

}

}

}

return degree;

}

基本處理操作就這么多渣慕,僅供參考嘶炭。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市逊桦,隨后出現(xiàn)的幾起案子眨猎,更是在濱河造成了極大的恐慌,老刑警劉巖强经,帶你破解...
    沈念sama閱讀 221,430評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件睡陪,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)兰迫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門信殊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人逮矛,你說(shuō)我怎么就攤上這事鸡号∽” “怎么了须鼎?”我有些...
    開封第一講書人閱讀 167,834評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)府蔗。 經(jīng)常有香客問(wèn)我晋控,道長(zhǎng),這世上最難降的妖魔是什么姓赤? 我笑而不...
    開封第一講書人閱讀 59,543評(píng)論 1 296
  • 正文 為了忘掉前任赡译,我火速辦了婚禮,結(jié)果婚禮上不铆,老公的妹妹穿的比我還像新娘蝌焚。我一直安慰自己,他們只是感情好誓斥,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評(píng)論 6 397
  • 文/花漫 我一把揭開白布只洒。 她就那樣靜靜地躺著,像睡著了一般劳坑。 火紅的嫁衣襯著肌膚如雪毕谴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,196評(píng)論 1 308
  • 那天距芬,我揣著相機(jī)與錄音涝开,去河邊找鬼。 笑死框仔,一個(gè)胖子當(dāng)著我的面吹牛舀武,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播离斩,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼银舱,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了捐腿?” 一聲冷哼從身側(cè)響起纵朋,我...
    開封第一講書人閱讀 39,671評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茄袖,沒想到半個(gè)月后操软,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,221評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡宪祥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評(píng)論 3 340
  • 正文 我和宋清朗相戀三年聂薪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了家乘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,444評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡藏澳,死狀恐怖仁锯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情翔悠,我是刑警寧澤业崖,帶...
    沈念sama閱讀 36,134評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站蓄愁,受9級(jí)特大地震影響双炕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撮抓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評(píng)論 3 333
  • 文/蒙蒙 一妇斤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧丹拯,春花似錦站超、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至剑刑,卻和暖如春媳纬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背施掏。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工钮惠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人七芭。 一個(gè)月前我還...
    沈念sama閱讀 48,837評(píng)論 3 376
  • 正文 我出身青樓素挽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親狸驳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子预明,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容