2012-11-22 20 views
9

जब मैं गैलरी से एंड्रॉइड एप्लिकेशन में अपने इमेजव्यू पर सेट करने के लिए एक छवि चुनता हूं तो मुझे लगता है कि यह 180 या 270 या 90 डिग्री से उलटा हुआ है।
तो इसे जांचने/हल करने के लिए मैंने EXIF उन्मुखीकरण का उपयोग किया, लेकिन यह हमेशा मुझे "0" देता है।गैलरी ओरिएंटेशन से चुनी गई एंड्रॉइड छवि हमेशा 0 है: Exif TAG

मैं समस्या क्या प्राप्त करने में सक्षम नहीं हूं।

कोड:

Uri selectedImageUri = data.getData(); 

        absolutePath = selectedImageUri.getPath(); 

        exifMedia = new ExifInterface(absolutePath); 
        String exifOrint = exifMedia.getAttribute(ExifInterface.TAG_ORIENTATION); 

        exifOrientation = Integer.parseInt(exifOrint); 

        System.out.println("Orientation Tag is:"+exifOrientation); 

        /** Convert URI into byte */ 
        ContentResolver cr = getBaseContext() 
        .getContentResolver(); 
        InputStream inputStream = cr 
        .openInputStream(selectedImageUri); 
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 

        rotatedBMP = getResizedBitmapImage(
          bitmap, 100, 100,exifOrientation); 

        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, 
          stream); 
        byteArray = stream.toByteArray(); 
        mimProfileImageBrowse.setImageBitmap(rotatedBMP); 

विधि:

public Bitmap getResizedBitmapImage(Bitmap bm, int newHeight, int newWidth, int exifOrientation) { 
     int width = bm.getWidth(); 
     int height = bm.getHeight(); 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 
     /** 
     * create a matrix for the manipulation 
     */ 

     Matrix matrix = new Matrix(); 
     /** 
     * resize the bit map 
     */ 


     /* 

     1 = Horizontal (normal) 
2 = Mirror horizontal 
3 = Rotate 180 
4 = Mirror vertical 
5 = Mirror horizontal and rotate 270 CW 
6 = Rotate 90 CW 
7 = Mirror horizontal and rotate 90 CW 
8 = Rotate 270 CW 

     */ 


     switch (exifOrientation) { 
     case ExifInterface.ORIENTATION_ROTATE_270: 

      rotate = 270; 

      break; 


     case ExifInterface.ORIENTATION_ROTATE_180: 

      rotate = 180; 


      break; 

     case ExifInterface.ORIENTATION_ROTATE_90: 

      rotate = 90; 

      break; 

     case ExifInterface.ORIENTATION_TRANSPOSE: 

      rotate = 45; 

      break; 

     default: 
      break; 
     } 


     matrix.postScale(scaleWidth, scaleHeight); 
     matrix.postRotate(rotate); 

     /** 
     * recreate the new Bitmap 
     */ 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 
     resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true); 

     return resizedBitmap; 

    } 

कृपया मदद करते हैं। matrix.postROTATE बिटमैप्स घूर्णन नहीं कर रहा है। मुझे नहीं पता क्यों।

धन्यवाद

उत्तर

24

के बाद आप

Uri selectedImageUri = data.getData(); 
Bitmap bitmap = scaleImage(this,selectedImageUri); 

दो अपनी गतिविधि कर रहे हैं में शामिल करने के लिए काम करता है,

public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException { 
     InputStream is = context.getContentResolver().openInputStream(photoUri); 
     BitmapFactory.Options dbo = new BitmapFactory.Options(); 
     dbo.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(is, null, dbo); 
     is.close(); 

     int rotatedWidth, rotatedHeight; 
     int orientation = getOrientation(context, photoUri); 

     if (orientation == 90 || orientation == 270) { 
      rotatedWidth = dbo.outHeight; 
      rotatedHeight = dbo.outWidth; 
     } else { 
      rotatedWidth = dbo.outWidth; 
      rotatedHeight = dbo.outHeight; 
     } 

     Bitmap srcBitmap; 
     is = context.getContentResolver().openInputStream(photoUri); 
     if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { 
      float widthRatio = ((float) rotatedWidth)/((float) MAX_IMAGE_DIMENSION); 
      float heightRatio = ((float) rotatedHeight)/((float) MAX_IMAGE_DIMENSION); 
      float maxRatio = Math.max(widthRatio, heightRatio); 

      // Create the bitmap from file 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = (int) maxRatio; 
      srcBitmap = BitmapFactory.decodeStream(is, null, options); 
     } else { 
      srcBitmap = BitmapFactory.decodeStream(is); 
     } 
     is.close(); 

     /* 
     * if the orientation is not 0 (or -1, which means we don't know), we 
     * have to do a rotation. 
     */ 
     if (orientation > 0) { 
      Matrix matrix = new Matrix(); 
      matrix.postRotate(orientation); 

      srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), 
        srcBitmap.getHeight(), matrix, true); 
     } 

     String type = context.getContentResolver().getType(photoUri); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     if (type.equals("image/png")) { 
      srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
     } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { 
      srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     } 
     byte[] bMapArray = baos.toByteArray(); 
     baos.close(); 
     return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); 
    } 

    public static int getOrientation(Context context, Uri photoUri) { 
     /* it's on the external media. */ 
     Cursor cursor = context.getContentResolver().query(photoUri, 
       new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); 

     if (cursor.getCount() != 1) { 
      return -1; 
     } 

     cursor.moveToFirst(); 
     return cursor.getInt(0); 
    } 

प्राप्त करने के लिए चयनित चित्र के उरी इस विधियों,
इस तरह का उपयोग कर नीचे कार्यों कॉल मिलता है,

public static Uri getImageContentUri(Context context, File imageFile) { 
     String filePath = imageFile.getAbsolutePath(); 
     Cursor cursor = context.getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       new String[] { MediaStore.Images.Media._ID }, 
       MediaStore.Images.Media.DATA + "=? ", 
       new String[] { filePath }, null); 
     if (cursor != null && cursor.moveToFirst()) { 
      int id = cursor.getInt(cursor 
        .getColumnIndex(MediaStore.MediaColumns._ID)); 
      Uri baseUri = Uri.parse("content://media/external/images/media"); 
      return Uri.withAppendedPath(baseUri, "" + id); 
     } else { 
      if (imageFile.exists()) { 
       ContentValues values = new ContentValues(); 
       values.put(MediaStore.Images.Media.DATA, filePath); 
       return context.getContentResolver().insert(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
      } else { 
       return null; 
      } 
     } 
    } 
: फ़ाइल उपयोग इस से उरी

प्रश्न। क्या आप मुझे बता सकते हैं कि "MAX_IMAGE_DIMENSION" क्या है? - Dhrupal मार्च 13 '13 ए अपने अपने imageView आकार या छवि का आकार तुम क्या दिखाना चाहते हैं .. जो भी हो 60X60 तो 60 है, 100x100 तो 100 @Dhrupal

+0

मैंने EXIF ​​समस्या हल कर दी है। क्या आप कृपया जांच सकते हैं कि बिटमैप घूर्णन क्यों नहीं कर रहा है। यह सभी लाइनों को सही ढंग से निष्पादित कर रहा है लेकिन यह बिटमैप –

+0

को घुमा नहीं रहा है मैंने यह भी किया है। अब घूर्णन। धन्यवाद –

+0

क्या आप मुझे बता सकते हैं कि "MAX_IMAGE_DIMENSION" क्या है? – Dhrupal

2

मुझे लगता है कि आप का उपयोग करना चाहिए नहीं "selectedImageUri.getPath()" पथ प्राप्त करने के बाद से यह वास्तव में छवि फ़ाइल पथ वापस नहीं करता है। इसके बजाय आपको MediaStore.Images.ImageColumns.DATA के मान से पूछने के लिए ContentResolver का उपयोग करना चाहिए जो छवि का वास्तविक पथ है।

असल में यदि आप केवल उन्मुखीकरण प्राप्त करना चाहते हैं, तो आपको इसे Exif से प्राप्त करने की आवश्यकता नहीं है, बस MediaStore.Images.ImageColumns.ORIENTATION के मान से पूछें कि आप इसे दे सकते हैं।

+0

मैंने इसे सही बना दिया है। लेकिन एक और समस्या यह है कि मेरा matrix.postRotate निष्पादित हो रहा है लेकिन मेरा बिटमैप –

+0

घूर्णन नहीं कर रहा है मैंने इसे भी किया है। अब घूर्णन। धन्यवाद –

+0

कूल। बस उत्सुक, आप दोबारा आकार बदलते बिटमैप को क्यों असाइन करते हैं? बिटमैप का आकार बदल गया बिटमैप = बिटमैप.क्रेट बिटमैप (बीएम, 0, 0, चौड़ाई, ऊंचाई, मैट्रिक्स, झूठा); आकार बदल गया बिटमैप = बिटमैप .createScaledBitmap (बीएम, 65, 65, सच); – Neil

2

यह पूछताछ के बाद से लंबे समय से किया गया है पर आधारित है। लेकिन मैं एक ही समस्या के साथ गिनती हुई, और इसे हल किया। जब आप exifInterface के नियंत्रक में फ़ाइल पथ डालते हैं, तो वास्तविक पथ का उपयोग करें, जैसे "/storage/sdcard0/DCIM/Camera/blahbalh.jpg", कुछ सामग्री/सामग्री का उपयोग न करें:/मीडिया/बाहरी/छवियों/मीडिया/ब्लाह, whiich देता है हमेशा 0 जानकारी। उम्मीद है कि किसी की मदद की जाएगी। Regrads।

ओह। नीचे एक ही दृष्टिकोण है। और यहां और अधिक है। इसका उपयोग करें।नीचे दिए गए कोड

/** 
* @ref http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore 
* @param contentUri 
* @return 
*/ 
public String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 
+0

यह ' kitkat – Piotr

+0

@Piotr पर आपका काम क्या है? – minimanimo

+0

एलजी जी 2। यह बस शून्य – Piotr

1

कोशिश: - विधि नीचे

ExifInterface exif = new ExifInterface(filename); 
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 
Bitmap bmRotated = rotateBitmap(bitmap, orientation); 

कॉल: -

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) { 

    try{ 
     Matrix matrix = new Matrix(); 
     switch (orientation) { 
      case ExifInterface.ORIENTATION_NORMAL: 
       return bitmap; 
      case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: 
       matrix.setScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       matrix.setRotate(180); 
       break; 
      case ExifInterface.ORIENTATION_FLIP_VERTICAL: 
       matrix.setRotate(180); 
       matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_TRANSPOSE: 
       matrix.setRotate(90); 
       matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       matrix.setRotate(90); 
       break; 
      case ExifInterface.ORIENTATION_TRANSVERSE: 
       matrix.setRotate(-90); 
       matrix.postScale(-1, 1); 
       break; 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       matrix.setRotate(-90); 
       break; 
      default: 
       return bitmap; 
     } 
     try { 
      Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
      bitmap.recycle(); 
      return bmRotated; 
     } 
     catch (OutOfMemoryError e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
    return bitmap; 
} 
1

आप getRealPathFromURI समारोह का उपयोग करना चाहिए:

public static String getRealPathFromURI(Context context, Uri uri, 
      String data) { 

    String[] largeFileProjection = { MediaStore.Images.ImageColumns._ID, 
      MediaStore.Images.ImageColumns.DATA }; 

    String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC"; 
    Cursor myCursor = context.getContentResolver().query(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      largeFileProjection, null, null, largeFileSort); 
    String largeImagePath = ""; 
    try { 
     myCursor.moveToFirst(); 
     largeImagePath = myCursor 
       .getString(myCursor 
         .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)); 
    } finally { 
     myCursor.close(); 
    } 

    return largeImagePath; 
} 
+0

यह उत्तर मेरे लिए काम करता है 'दस्तावेज़फाइलपाथ = getRealPathFromURI (getAplicationContext(), uri, data.getDataString()); ' – Alexiscanny

-1

जब छवि पर कब्जा कर लिया है, यह रोटेटियो के बारे में जानकारी के रूप में एक exif डेटा स्टोर करता है एन प्रकार (चित्र - परिदृश्य)।

ExifInterface exifData = new ExifInterface(uri); 
     int orientation = exifData.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
     Matrix matrix; 
     switch (orientation) { 
     case 6: 
      matrix = new Matrix(); 
      matrix.postRotate(90); 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
      break; 
     case 3: 
      matrix = new Matrix(); 
      matrix.postRotate(180); 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
      break; 
     case 8: 
      matrix = new Matrix(); 
      matrix.postRotate(270); 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
      break; 
     default: 
      break; 
-2

आप पहले से ही जांच की है यदि आप अपने मेनिफ़ेस्ट में लिखें और पढ़ें अनुमति सेट था: तो केवल एक चीज की जांच करनी है EXIF ​​डेटा है? क्योंकि अगर आपने उन्हें सेट नहीं किया है तो आप अपने बिटमैप के एक्सआईएफ डेटा तक पहुंच नहीं सकते हैं, मैं एंड्रॉइड एम के बारे में बात कर रहा हूं यह अपने एक्सिफ़ दादा के आधार पर बिटमैप को घुमाने के लिए स्निपेट है।

public static Bitmap rotateBitmap(String filePath, Bitmap bitmap) { 
    ExifInterface exif; 
    try { 
     exif = new ExifInterface(filePath); 

     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     Log.d("EXIF", "Exif: " + orientation); 
     Matrix matrix = new Matrix(); 
     if (orientation == 6) { 
      matrix.postRotate(90); 
      Log.d("EXIF", "Exif: " + orientation); 
     } else if (orientation == 3) { 
      matrix.postRotate(180); 
      Log.d("EXIF", "Exif: " + orientation); 
     } else if (orientation == 8) { 
      matrix.postRotate(270); 
      Log.d("EXIF", "Exif: " + orientation); 
     } 
     return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 


} 
0

मैं या तो कर्सर या Exif इंटरफ़ेस विधि मेरे लिए काम करने के लिए नहीं मिल सका। मैं सैमसंग पर दोनों फ्रंट और बैक कैमरों के लिए विश्वसनीय रोटेशन नंबर प्राप्त करने में सक्षम नहीं था।

एक समाधान है जो सैमसंग और अन्य प्रमुख निर्माताओं के लिए आगे और पीछे कैमरे में काम करता है की तलाश में उन लोगों के लिए, nvhausid द्वारा इस उत्तर भयानक है:

https://stackoverflow.com/a/18915443/6080472

जो लोग क्लिक करने के लिए नहीं करना चाहते हैं के लिए के माध्यम से, प्रासंगिक जादू कैमराइन्फो का उपयोग करने के बजाय EXIF ​​पर निर्भर है।

Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length); 
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
android.hardware.Camera.getCameraInfo(mCurrentCameraId, info); 
Bitmap bitmap = rotate(realImage, info.orientation); 

लिंक में पूरा कोड।

+0

यह सवाल गैलरी से छवि का चयन करने के बारे में है कैमरा नहीं। –