2017-02-23 45 views
2

我把照片bt相機模式的風景,上傳到服務器後...這是風景。爲什麼圖像自動旋轉設置爲Imageview與畢加索

但是,當我負荷的ImageView則顯示垂直像下面的圖像: enter image description here

我用畢加索加載圖像Imageview.I要像ImageView的原始圖像顯示... 請建議對我.. .tks太多了!

public static void makeImageRequest(Context context, ImageView imageView, final String imageUrl, ProgressBar progressBar) { 
     final int defaultImageResId = R.drawable.ic_member; 
     Picasso.with(context) 
       .load(imageUrl) 
       .error(defaultImageResId) 
       .resize(80, 80) 
       .into(imageView); 
       } 

的ImageView:

<ImageView 
           android:layout_centerInParent="true" 
           android:padding="@dimen/_8sdp" 
           android:id="@+id/img_photo" 
           android:layout_width="@dimen/_80sdp" 
           android:layout_height="@dimen/_80sdp" 
           android:layout_gravity="center" 
           android:scaleType="fitCenter" 
           android:adjustViewBounds="true" /> 

網址:

https://arubaitobsv.s3-ap-northeast-1.amazonaws.com/images/1487816629838-20170223_092312_HDR.jpg 
+0

請發表您的相關代碼 –

回答

7

問題的問題d here

90度的圖像從具有以下EXIF數據網絡未來畢加索自動旋轉:

Resolution : 3264 x 2448 
Orientation : rotate 90 

試試這個代碼與畢加索:

Picasso.with(MainActivity.this) 
    .load(imageURL) // web image url 
    .fit().centerInside() 
    .transform(transformation) 
    .rotate(90)     //if you want to rotate by 90 degrees 
    .error(R.drawable.ic_launcher) 
    .placeholder(R.drawable.ic_launcher) 
    .into(imageview) 
    }); 

您還可以使用滑翔:

dependencies { 
    // Your app's other dependencies 
    compile 'com.github.bumptech.glide:glide.3.7.0' 
} 

laod image using:

Glide.with(this).load("image_url").into(imageView); 
+0

Tks you so much ...我明白了,這是我的正確答案... – cheng

+0

Tks u r welcome,upvote the answers if if it helps .. – rafsanahmad007

+0

是的。這也適用於Google驅動器圖像。但如何從庫中獲取位圖圖像? @ rafsanahmad007。 – pratik03

6
public class ImageRotationDetectionHelper { 

    public static int getCameraPhotoOrientation(String imageFilePath) { 
     int rotate = 0; 
     try { 

      ExifInterface exif; 

      exif = new ExifInterface(imageFilePath); 
      String exifOrientation = exif 
        .getAttribute(ExifInterface.TAG_ORIENTATION); 
      Log.d("exifOrientation", exifOrientation); 
      int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION, 
        ExifInterface.ORIENTATION_NORMAL); 
      Log.d(ImageRotationDetectionHelper.class.getSimpleName(), "orientation :" + orientation); 
      switch (orientation) { 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        rotate = 270; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        rotate = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        rotate = 90; 
        break; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return rotate; 
    } 
} 
+0

使用上面的代碼檢查圖像的旋轉角度,並在必要 –

+0

韓國社交協會,但像圖片的URL圖像顯示我拍照設備上的旋轉它,但是當我設置imageview的再不像,它是旋轉的。 – cheng

+0

要麼你需要檢查旋轉的圖像,而上傳或檢查後檢查旋轉和設置圖像任何一種方式將工作 –

1

嘗試這種方式,將工作

public class MainActivity extends Activity { 

    private ImageView imgFromCameraOrGallery; 
    private Button btnCamera; 
    private Button btnGallery; 

    private String imgPath; 
    final private int PICK_IMAGE = 1; 
    final private int CAPTURE_IMAGE = 2; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     imgFromCameraOrGallery = (ImageView) findViewById(R.id.imgFromCameraOrGallery); 
     btnCamera = (Button) findViewById(R.id.btnCamera); 
     btnGallery = (Button) findViewById(R.id.btnGallery); 

     btnCamera.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); 
       startActivityForResult(intent, CAPTURE_IMAGE); 
      } 
     }); 

     btnGallery.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE); 
      } 
     }); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == CAPTURE_IMAGE) { 
       setCapturedImage(getImagePath()); 
      } else if (requestCode == PICK_IMAGE) { 
       imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData()))); 
      } 
     } 

    } 

    private String getRightAngleImage(String photoPath) { 

     try { 
      ExifInterface ei = new ExifInterface(photoPath); 
      int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
      int degree = 0; 

      switch (orientation) { 
       case ExifInterface.ORIENTATION_NORMAL: 
        degree = 0; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        degree = 90; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        degree = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        degree = 270; 
        break; 
       case ExifInterface.ORIENTATION_UNDEFINED: 
        degree = 0; 
        break; 
       default: 
        degree = 90; 
      } 

      return rotateImage(degree,photoPath); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return photoPath; 
    } 

    private String rotateImage(int degree, String imagePath){ 

     if(degree<=0){ 
      return imagePath; 
     } 
     try{ 
      Bitmap b= BitmapFactory.decodeFile(imagePath); 

      Matrix matrix = new Matrix(); 
      if(b.getWidth()>b.getHeight()){ 
       matrix.setRotate(degree); 
       b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), 
         matrix, true); 
      } 

      FileOutputStream fOut = new FileOutputStream(imagePath); 
      String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1); 
      String imageType = imageName.substring(imageName.lastIndexOf(".") + 1); 

      FileOutputStream out = new FileOutputStream(imagePath); 
      if (imageType.equalsIgnoreCase("png")) { 
       b.compress(Bitmap.CompressFormat.PNG, 100, out); 
      }else if (imageType.equalsIgnoreCase("jpeg")|| imageType.equalsIgnoreCase("jpg")) { 
       b.compress(Bitmap.CompressFormat.JPEG, 100, out); 
      } 
      fOut.flush(); 
      fOut.close(); 

      b.recycle(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return imagePath; 
    } 

    private void setCapturedImage(final String imagePath){ 
     new AsyncTask<Void,Void,String>(){ 
      @Override 
      protected String doInBackground(Void... params) { 
       try { 
        return getRightAngleImage(imagePath); 
       }catch (Throwable e){ 
        e.printStackTrace(); 
       } 
       return imagePath; 
      } 

      @Override 
      protected void onPostExecute(String imagePath) { 
       super.onPostExecute(imagePath); 
       imgFromCameraOrGallery.setImageBitmap(decodeFile(imagePath)); 
      } 
     }.execute(); 
    } 

    public Bitmap decodeFile(String path) { 
     try { 
      // Decode deal_image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, o); 
      // The new size we want to scale to 
      final int REQUIRED_SIZE = 1024; 

      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale *= 2; 
      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeFile(path, o2); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public String getAbsolutePath(Uri uri) { 
     if(Build.VERSION.SDK_INT >= 19){ 
      String id = ""; 
      if(uri.getLastPathSegment().split(":").length > 1) 
       id = uri.getLastPathSegment().split(":")[1]; 
      else if(uri.getLastPathSegment().split(":").length > 0) 
       id = uri.getLastPathSegment().split(":")[0]; 
      if(id.length() > 0){ 
       final String[] imageColumns = {MediaStore.Images.Media.DATA }; 
       final String imageOrderBy = null; 
       Uri tempUri = getUri(); 
       Cursor imageCursor = getContentResolver().query(tempUri, imageColumns, MediaStore.Images.Media._ID + "=" + id, null, imageOrderBy); 
       if (imageCursor.moveToFirst()) { 
        return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
       }else{ 
        return null; 
       } 
      }else{ 
       return null; 
      } 
     }else{ 
      String[] projection = { MediaStore.MediaColumns.DATA }; 
      Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
      if (cursor != null) { 
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
       cursor.moveToFirst(); 
       return cursor.getString(column_index); 
      } else 
       return null; 
     } 

    } 

    private Uri getUri() { 
     String state = Environment.getExternalStorageState(); 
     if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) 
      return MediaStore.Images.Media.INTERNAL_CONTENT_URI; 

     return MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    } 

    public Uri setImageUri() { 
     Uri imgUri; 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/",getString(R.string.app_name) + Calendar.getInstance().getTimeInMillis() + ".png"); 
      imgUri = Uri.fromFile(file); 
      imgPath = file.getAbsolutePath(); 
     }else { 
      File file = new File(getFilesDir() ,getString(R.string.app_name) + Calendar.getInstance().getTimeInMillis()+ ".png"); 
      imgUri = Uri.fromFile(file); 
      this.imgPath = file.getAbsolutePath(); 
     } 
     return imgUri; 
    } 

    public String getImagePath() { 
     return imgPath; 
    } 
} 
+0

你答是正確的,但因此他只需要檢查圖像輪替],並可以使用畢加索 –

+0

'畢加索 。隨着(上下文) .load(UsageExampleListViewAdapter.eatFoodyImages [0]) .rotate旋轉圖像(他是用畢加索90f) .into(imageViewSimpleRotate);' –

+0

Tks但我認爲:mages捕獲後是正確的,上傳到服務器後也是如此。但是隻有設置成Imageview是因爲它是旋轉的。 我只希望它顯示像原始圖像 – cheng