2012-09-06 14 views
5

Estoy utilizando exifInterface para el código de problema de rotación de fotos que se muestra a continuación: Enfrentando el problema de orientación de la imagen capturada desde la cámara.Enfrentando el problema de orientación con la imagen capturada de la cámara en los teléfonos Android

  • Crear un mapa de bits del archivo
Bitmap b = BitmapFactory.decodeFile(imagePath); 
  • cambiar el tamaño del mapa de bits mediante el escalado a nivel apropiado
int width = b.getWidth(); 
int height = b.getHeight(); 
int newWidth = 150; 
int newHeight = 150; 
float scaleWidth = ((float) newWidth)/width; 
float scaleHeight = ((float) newHeight)/height; 
Matrix matrix = new Matrix(); 
matrix.postScale(scaleWidth, scaleHeight); 
// Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true); 
// resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out); 
  • orientación de la manija de la imagen
ExifInterface exif = new ExifInterface(imagePath); 
    String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
    if (orientation.equals(ExifInterface.ORIENTATION_NORMAL)) { 
      // Do nothing. The original image is fine. 
    } else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_90+"")) { 
      matrix.postRotate(90); 
    } else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_180+"")) { 
      matrix.postRotate(180); 
    } else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_270+"")) { 
      matrix.postRotate(270); 
} 
  • Guardar el nuevo mapa de bits
out = new FileOutputStream(new File("some output file path")); 
    Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true); 
    resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out); 

Este código no funciona para la emisión de rotación resolución, por favor, dame únicamente. En los dispositivos LG su interfaz externa siempre devuelve la orientación 0, los dispositivos Samsung devuelven 6 y 1.

Cómo solucionar este problema con todos los dispositivos como htc, Motorola, Samsung, Sony y LG.

Estoy agradecido a todos ustedes por favor ayúdenme.

Respuesta

7

Puede usar esta función para hacer lo mismo que necesita. Tenga esta función en su actividad o en cualquier clase de utilidad y llámela para obtener un mapa de bits de la ruta del archivo.

He estado usando esta función en mi aplicación y mis dispositivos de prueba principales son LG.

public static Bitmap decodeFile(String path) { 
    int orientation; 
    try { 
     if (path == null) { 
      return null; 
     } 
     // decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 8; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE 
        || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale++; 
     } 
     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     Bitmap bm = BitmapFactory.decodeFile(path, o2); 
     Bitmap bitmap = bm; 

     ExifInterface exif = new ExifInterface(path); 
     orientation = exif 
       .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
     Log.e("orientation", "" + orientation); 
     Matrix m = new Matrix(); 

     if ((orientation == 3)) { 
      m.postRotate(180); 
      m.postScale((float) bm.getWidth(), (float) bm.getHeight()); 
      // if(m.preRotate(90)){ 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), 
        bm.getHeight(), m, true); 
      return bitmap; 
     } else if (orientation == 6) { 
      m.postRotate(90); 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), 
        bm.getHeight(), m, true); 
      return bitmap; 
     } 
     else if (orientation == 8) { 
      m.postRotate(270); 
      Log.e("in orientation", "" + orientation); 
      bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), 
        bm.getHeight(), m, true); 
      return bitmap; 
     } 
     return bitmap; 
    } catch (Exception e) { 
    } 
    return null; 
} 
+0

¿Has probado en HTC? – user991429

+0

Sí, también pruebo la aplicación con HTC Explorer y funciona bien como LG Optimus Black. @ user991429 – MKJParekh

+0

@MKJParekh: Buen trabajo, gracias :) – Aerrow

Cuestiones relacionadas