2012-11-17 16 views
8

La foto sta ruotando di 90 gradi durante l'acquisizione da fotocamera in samsung mobile resto di altri cellulari (HTC) funziona correttamente. Per favore aiutami per questo.Cattura foto di 90 gradi in samsung mobile

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, IMAGE_CAPTURE); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);  
     try 
    { 
    if (requestCode == IMAGE_CAPTURE) { 
     if (resultCode == RESULT_OK){ 

     Uri contentUri = data.getData(); 
     if(contentUri!=null) 
     { 
     String[] proj = { MediaStore.Images.Media.DATA };   
      Cursor cursor = managedQuery(contentUri, proj, null, null, null);   
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
     cursor.moveToFirst();   
     imageUri = Uri.parse(cursor.getString(column_index)); 
     } 

     tempBitmap = (Bitmap) data.getExtras().get("data"); 
     mainImageView.setImageBitmap(tempBitmap); 
     isCaptureFromCamera = true; 
    } 
} 
+0

Ti aspetti l'orientamento dell'immagine verticale? –

+0

No, mi aspetto lo stesso orientamento dell'immagine che ho acquisito per acquisire la foto in modalità verticale, quindi dovrebbe essere in verticale e lo stesso per il panorama ... per favore aiutatemi –

+1

Ci sono numerosi bug diversi con l'orientamento verticale della fotocamera su diversi dispositivi Android, tra cui Samsung. Se possibile, usa l'orientamento orizzontale e la modalità ritratto falso usando gli elementi dell'interfaccia utente ruotati, come fa l'app fotocamera originale. –

risposta

11

Alcuni dispositivi ruotano l'immagine in base all'orientamento del dispositivo.

qui devo scrivere un metodo comune per ottenere l'orientamento e ricevere immagini in scala di destra

public Bitmap decodeFile(String path) {//you can provide file path here 
     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 = 0; 
      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("ExifInteface .........", "rotation ="+orientation); 

//   exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90); 

      Log.e("orientation", "" + orientation); 
      Matrix m = new Matrix(); 

      if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) { 
       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 == ExifInterface.ORIENTATION_ROTATE_90) { 
       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 == ExifInterface.ORIENTATION_ROTATE_270) { 
       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; 
     } 

    } 

EDIT:

Questo codice non è ottimizzato, ho appena mostrano il codice logico dal mio uno del progetto di test.

+0

La sua soluzione davvero bella, e funziona bene. Molte grazie. – Hasmukh

+0

funziona bene ma fornisce "Eccezione memoria" mentre sto acquisendo un'istantanea continua in Samsung Galaxy tab2 (7 ") .Puoi dare qualche soluzione – Hasmukh

+0

@Hasmukh puoi cambiare la dimensione della scala bitmap, in questo codice i lo ha reso affidabile – dhams

0
public static Bitmap rotateBitmap(Bitmap b, float degrees) { 
    Matrix m = new Matrix(); 
    if (degrees != 0) { 
     // clockwise 
     m.postRotate(degrees, (float) b.getWidth()/2, 
       (float) b.getHeight()/2); 
    } 

    try { 
     Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), 
       b.getHeight(), m, true); 
     if (b != b2) { 
      b.recycle(); 
      b = b2; 
     } 
    } catch (OutOfMemoryError ex) { 
     // We have no memory to rotate. Return the original bitmap. 
    } 
    return b; 
} 
0

Se è davvero un bug allora si può avere ruotare manualmente di nuovo al paesaggio. I dati bitmap hanno sempre una larghezza e un'altezza, basta dare un'occhiata ai numeri e se la larghezza è inferiore all'altezza ruota l'immagine come da risposta di alistair3408.

2

Un'altra cosa che è possibile aggiungere alle soluzioni di cui sopra è "samsung".contentEquals(Build.MANUFACTURER). Se sai che il tuo problema riguarda solo i dispositivi Samsung, potresti essere ragionevolmente sicuro di dover ruotare l'immagine restituita (solo) if ("samsung".contentEquals(Build.MANUFACTURER) && getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT // && width > height//) // here you know you need to rotate

Si potrebbe essere "ragionevolmente" sicuri che la rotazione sia garantita in quel momento.

+0

non è la soluzione. da Samsung hai sempre LARGHEZZA> ALTEZZA. Bug solo con modalità Portrate. – Peter

Problemi correlati