亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android獲取相機位圖的方向?并向后旋轉-90度

Android獲取相機位圖的方向?并向后旋轉-90度

呼啦一陣風 2019-11-12 11:13:59
我有以下代碼://choosed a picturepublic void onActivityResult(int requestCode, int resultCode, Intent data) {    if (resultCode == RESULT_OK) {        if (requestCode == ImageHelper.SELECT_PICTURE) {            String picture           = "";            Uri selectedImageUri     = data.getData();            //OI FILE Manager            String filemanagerstring = selectedImageUri.getPath();            //MEDIA GALLERY            String selectedImagePath = ImageHelper.getPath(mycontext, selectedImageUri);            picture=(selectedImagePath!=null)?selectedImagePath:filemanagerstring;...這只是圖庫中的圖片選擇器。這很好,但是當我在imageview上打開該圖片時,使用相機在“肖像模式”下拍攝的圖像看起來不錯,但是使用相機在“景觀模式”下拍攝的圖像以-90度打開。如何旋轉這些圖片?    Bitmap output       = Bitmap.createBitmap(newwidth, newheight, Config.ARGB_8888);    Canvas canvas       = new Canvas(output);我嘗試了這個:Log.e("w h", bitmap.getWidth()+" "+bitmap.getHeight());if (bitmap.getWidth()<bitmap.getHeight()) canvas.rotate(-90);但這不起作用,所有圖像尺寸均為:* 2560 1920像素(全部為肖像和橫向模式)我該怎么做才能旋轉回LANDSCAPE圖片?謝謝萊斯利
查看完整描述

3 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

如果使用數碼相機或智能手機拍攝照片,則旋轉通常作為圖像文件的一部分存儲在照片的Exif數據中。您可以使用Android讀取圖像的Exif元數據ExifInterface。


首先,創建ExifInterface:


ExifInterface exif = new ExifInterface(uri.getPath());

接下來,找到當前旋轉:


int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  

將exif旋轉轉換為度數:


int rotationInDegrees = exifToDegrees(rotation);

哪里


private static int exifToDegrees(int exifOrientation) {        

    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } 

    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; } 

    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }            

    return 0;    

 }

然后,以圖像的實際旋轉為參考點,使用來旋轉圖像Matrix。


Matrix matrix = new Matrix();

if (rotation != 0) {matrix.preRotate(rotationInDegrees);}

使用將Bitmap.createBitmapa Matrix作為參數的方法來創建新的旋轉圖像:


Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

其中Matrix m擁有新的輪換:


Bitmap adjustedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);


查看完整回答
反對 回復 2019-11-12
  • 3 回答
  • 0 關注
  • 828 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號