2011-07-25 20 views
13

fotoğraf çekmek için kamera kullanan android bir uygulamada çalışıyorum.Film başlatmak için bir intentACTION_IMAGE_CAPTURE kullanıyorum:ACTION_IMAGE_CAPTURE amacı ile başlatılan android kameranın yönlendirmesini ayarla

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File image=new File(Environment.getExternalStorageDirectory(),"PhotoContest.jpg"); 
     camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(image)); 
     imageUri=Uri.fromFile(image); 
     startActivityForResult(camera,1); 

public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode){ 
     case 1: 
      if (resultCode == Activity.RESULT_OK) { 
        selectedImage = imageUri; 
        getContentResolver().notifyChange(selectedImage, null); 
        image= (ImageView) findViewById(R.id.imageview); 
        ContentResolver cr = getContentResolver(); 
        Bitmap bitmap; 
        try { 
         bitmap = android.provider.MediaStore.Images.Media 
         .getBitmap(cr, selectedImage); 
         image.setImageBitmap(bitmap); 
         Toast.makeText(this, selectedImage.toString(), 
           Toast.LENGTH_LONG).show(); 
        } catch (Exception e) { 
         Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
           .show(); 
         Log.e("Camera", e.toString()); 
        } 
       } 
      else 

     if(resultCode == Activity.RESULT_CANCELED) { 
        Toast.makeText(EditPhoto.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show(); 
       } 
     } 
} 

Sorun çekilen tüm fotoğrafların döndürülmesi 90 derece yatay olarak hizalanmış.

Bunu kendi manifest dosyama koydum:

 <activity android:name=".EditPhoto"> 
    android:screenOrientation="portrait" 
    </activity> 

Ama yine de sonuç yok! Herkes bana yardım edebilir mi ???

+1

Göndermiş olduğunuz kod, çekildikten sonra Görüntüyü almak içindir. Sadece portre veya manzara koyabileceğini sanmıyorum. Bunlar daha çok görüntünün yönelimiyle ilgilidir. Ancak kamera, kullanıcının dünyayı "normal" görünecek şekilde görüntüleyebilmesi için cihazı döndürmesini gerektirecek şekilde monte edilebilir ve döndürülebilir. Yani, portre derseniz bile, görüntü başka bir şekilde yansıtılabilir. SDK'nın sonraki sürümlerinde, kamerada setRotation (eski sürümlerde Parametreleri hacklemek zorunda) vardır. Görüntüde size yönlendirmeyi göstermek için EXIF ​​üstbilgileri olabilir. –

+0

Peki, çözüm nedir? – adrian

+0

Kullandığınız yakalama kodu olmadan söylemek zor. Bunu yazabilir misin? –

cevap

29

http://developer.android.com/reference/android/media/ExifInterface.html

http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION

Yani eğer

Activity.onActivityResult(data, request, result) { 
if (request == PHOTO_REQUEST && result == RESULT_OK) { 
    ... 
    Uri imageUri = ... 
    File imageFile = new File(imageUri.toString()); 
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
    int rotate = 0; 
    switch(orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     rotate-=90; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     rotate-=90; 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     rotate-=90; 
    } 
    Canvas canvas = new Canvas(bitmap); 
    canvas.rotate(rotate); 
} 

bu yardımı mu içinde hiç ?


Sadece burada işi yapmak için bir bütün "kategorisi" var, Greg harika cevap eklemek için: ...

public static int neededRotation(File ff) 
     { 
     try 
      { 

      ExifInterface exif = new ExifInterface(ff.getAbsolutePath()); 
      int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

      if (orientation == ExifInterface.ORIENTATION_ROTATE_270) 
       { return 270; } 
      if (orientation == ExifInterface.ORIENTATION_ROTATE_180) 
       { return 180; } 
      if (orientation == ExifInterface.ORIENTATION_ROTATE_90) 
       { return 90; } 
      return 0; 

      } catch (FileNotFoundException e) 
      { 
      e.printStackTrace(); 
      } catch (IOException e) 
      { 
      e.printStackTrace(); 
      } 
     return 0; 
     } 

daha fazla veya daha az bu gibi kullanmak istiyorum

public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
    if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK) 
     { 
     try 
      { 
      Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
        State.mainActivity.getContentResolver(), 
        Uri.fromFile(Utils.tempFileForAnImage()) ); 

      cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 320,320); 
      // NOTE incredibly useful trick for cropping/resizing square 
      // http://stackoverflow.com/a/17733530/294884 

      Matrix m = new Matrix(); 
      m.postRotate(Utils.neededRotation(Utils.tempFileForAnImage())); 

      cameraBmp = Bitmap.createBitmap(cameraBmp, 
        0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(), 
        m, true); 

      yourImageView.setImageBitmap(cameraBmp); 

      // to convert to bytes... 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      cameraBmp.compress(Bitmap.CompressFormat.JPEG, 75, baos); 
      //or say cameraBmp.compress(Bitmap.CompressFormat.PNG, 0, baos); 
      imageBytesRESULT = baos.toByteArray(); 

      } catch (FileNotFoundException e) 
      { 
      e.printStackTrace(); 
      } catch (IOException e) 
      { 
      e.printStackTrace(); 
      } 

     return; 
     } 

    } 

Gelecekte bazılarını yazarak kaydetmesini umarız.

+0

Kendi kameramı kurdum, Greg.Thank sen zaten – adrian

+0

Seni kıskanıyorum. :) Artık herkesin engellenemediğini duymak güzel. Sadece bitirmek için bunu yayınlayacağımı düşündüm. –

+0

Bana tuvalde nullpointer exeption vermek tuval = new Canvas (bitmap); – CoronaPintu

2

Yukarıdaki yanıt çok kapsamlıdır, ancak her bir durumun çalışması için özellikle Galeri veya Google Fotoğraflar gibi diğer kaynaklardan görüntülerle uğraşırken biraz daha fazla şey yapmak zorunda kaldım. İşte DetermineOrientation yöntemim. Bunun bulunduğu bir yardımcı program sınıfım var, bu yüzden Yönetilen Sorgusu kullanmak için Etkinliği geçmek zorundayım (btw kullanımdan kaldırıldıkça dikkatli olun). İki yöntemi kullanmamın nedeni, görüntünün kaynağına bağlı olarak, ExifInterface çalışmaz. Örneğin, bir fotoğraf çektiğimde, Exif iyi çalışıyor. Ancak, Galeri'den veya Google Drive'dan görüntüleri seçiyorsam, Exif çalışmaz ve her zaman 0 değerini döndürür. Bu, birilerine yardımcı olur.

public static int DetermineOrientation(Activity activity, Uri fileUri) 
{ 
    int orientation = -1; 
    int rotate = 0; 
    try { 

     ExifInterface exif = new ExifInterface(fileUri.getPath()); 
     orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     rotate = 0; 
     switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       rotate = 270; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       rotate = 180; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       rotate = 90; 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if(rotate == 0) 
    { 
     String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; 
     Cursor cur = activity.managedQuery(fileUri, orientationColumn, null, null, null); 
     orientation = -1; 
     if (cur != null && cur.moveToFirst()) { 
      orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); 
     } 

     if(orientation != -1) 
     { 
      rotate = orientation; 
     } 
    } 

    return rotate; 
} 
İlgili konular