2011-07-25 23 views
13

Tôi đang làm việc tại một ứng dụng trong Android mà sử dụng camera để chụp photos.For khởi động camera Tôi đang sử dụng một intentACTION_IMAGE_CAPTURE như thế này:bộ định hướng của máy ảnh android bắt đầu với ý định ACTION_IMAGE_CAPTURE

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(); 
       } 
     } 
} 

Các vấn đề là tất cả các bức ảnh được chụp được xoay với 90 độ theo chiều ngang.

Tôi cũng đã đưa điều này vào file manifest của tôi:

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

Nhưng vẫn không có kết quả Vì vậy, bất cứ ai có thể giúp tôi ???

+1

Mã bạn đăng là để nhận Hình ảnh sau khi đã chụp. Tôi không nghĩ rằng bạn có thể chỉ cần đặt chân dung hoặc phong cảnh. Chúng liên quan nhiều hơn đến hướng của khung nhìn. Nhưng máy ảnh có thể được gắn và xoay theo cách mà nó yêu cầu người dùng xoay thiết bị để có thể xem thế giới trong những gì có vẻ "bình thường" đối với họ. Vì vậy, ngay cả khi bạn nói chân dung hình ảnh có thể được chiếu theo một cách khác. Trong các phiên bản sau của SDK có một setRotation trên Camera (sử dụng phải hack các tham số trên các phiên bản cũ). Có thể có các tiêu đề EXIF ​​trong hình ảnh để cho bạn biết hướng. –

+0

Vậy giải pháp là gì !? – adrian

+0

Thật khó để nói mà không có mã chụp mà bạn sử dụng. Bạn có thể đăng bài đó không? –

Trả lời

29

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

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

Vì vậy, nếu trong

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); 
} 

Liệu sự giúp đỡ này ở tất cả?


Chỉ cần để thêm vào câu trả lời tuyệt vời của Greg, đây là một "thể loại" toàn bộ để thực hiện công việc:

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; 
     } 

bạn muốn sử dụng nó nhiều hơn hoặc ít hơn như thế này ...

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; 
     } 

    } 

Hy vọng nó sẽ giúp ai đó gõ một số trong tương lai.

+0

Tôi xây dựng máy ảnh của riêng mình, Greg.Cảm ơn bạn anyway – adrian

+0

Tôi không ghen tị với bạn. :) Thật tuyệt khi nghe tất cả đã được bỏ chặn ngay bây giờ. Chỉ cần nghĩ rằng tôi sẽ đăng này để hoàn thành. –

+0

Cung cấp cho tôi giả mạo nullpointer trong Canvas canvas = new Canvas (bitmap); – CoronaPintu

2

Câu trả lời ở trên rất kỹ lưỡng, nhưng tôi thấy tôi phải làm nhiều hơn một chút để mọi trường hợp có thể hoạt động, đặc biệt nếu bạn đang xử lý hình ảnh từ các nguồn khác như Thư viện hoặc Google Photos. Đây là phương pháp xác định của tôi. Tôi có một lớp tiện ích, nơi nó nằm ở vị trí nên tôi phải chuyển vào Activity để sử dụng managedQuery (mà btw bị phản đối nên sử dụng thận trọng). Lý do tôi phải sử dụng hai phương pháp là bởi vì, tùy thuộc vào nguồn gốc của hình ảnh, ExifInterface sẽ không hoạt động. Ví dụ, nếu tôi chụp ảnh máy ảnh, Exif hoạt động tốt. Tuy nhiên, nếu tôi cũng chọn hình ảnh từ Thư viện hoặc Google Drive, Exif không hoạt động và luôn trả về 0. Hy vọng điều này sẽ giúp người khác.

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; 
} 
Các vấn đề liên quan