2012-10-08 2 views
4

내 앱에서 모바일 갤러리 페이지에서 일부 이미지를 업로드해야합니다.인물 사진을 인물 사진으로 찍습니다.

저는 삼성 갤럭시 에이스를 사용하고 있으며 휴대 전화의 기본 카메라를 사용하여 세로 모드에서 일부 이미지를 캡처했습니다. 캡처 후 난 내 애플 리케이션에서 그 이미지를 열어 이미지보기에서 보여 주려고. 인물 모드에서 캡처 된 이미지는 이미지보기에서 가로로 보입니다. exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION)를 사용

내가

Matrix matrix = new Matrix(); 
         matrix.postRotate(90); 
         bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(HomePage._uri)); 
         bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
         i.setImageBitmap(bitmap); 

그러나 이후

이미지를 업로드, 내가 이미지보기에서 세로 모드에서 이미지를 표시하고 다음 코드를 사용하여 6

로 할 이미지 방향 값을 확인 내 응용 프로그램의 다른 활동에서 그것을 검색하면 다시 가로 모드로 보인다. 초상화 자체에서 이미지를 업로드하는 방법은 무엇입니까?

I have captured in Portrait , i have showed it in portrait by myself, while uploading it i need it to be in portrait itself, so that when i am retrieving it i can view it in portrait mode,

이 작업을 수행하는 방법을,

답변

1

내가 솔루션을 발견했다 (I의 힘이 내 응용 프로그램에서 카메라를 사용하여 캡처, 내가 응용 프로그램 외부에서 휴대폰의 기본 카메라를 사용하여 캡처) 갤러리에서 이미지를 가져 와서 업로드하십시오. 갤러리에서 선택한 일부 이미지는이 경우에 다음과 같은 솔루션은

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     intent.setType("image/*"); 
     startActivityForResult(intent, 2); 

다음 onActivityResult를 여기에

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode == Activity.RESULT_OK) 
     { 
      f(requestCode == 2) 
      { 
       try 
       { 
       String [] proj = { MediaStore.Images.Media.DATA }; 
       Cursor cursor = managedQuery(data.getData(), proj, null, null, null); 
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToFirst(); 
       pathInput = cursor.getString(column_index); 

       Appconstants.f = Environment.getExternalStorageDirectory() + "/tmp_siva.jpg"; 
       ImageUtils.resampleImageAndSaveToNewLocation(pathInput, Appconstants.f); 
       } 
       catch (Exception ex) 
       { 
       Log.e("Exception ex @ try catch",""+ex); 
       } 
      } 
      }    
    } 

는 ImageUtils 클래스

이다 갤러리에서

선택 이미지를 잘 작동, 회전 보일 수 있습니다

public class ImageUtils 
{ 
    private ImageUtils() 
    { 
    } 

    public static void resampleImageAndSaveToNewLocation(String pathInput, String pathOutput) throws Exception 
    { 
     Bitmap bmp = resampleImage(pathInput, 800); 

     OutputStream out = new FileOutputStream(pathOutput); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    } 

    public static Bitmap resampleImage(String path, int maxDim) throws Exception 
    {   
     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     bfo.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, bfo); 

     BitmapFactory.Options optsDownSample = new BitmapFactory.Options(); 
     optsDownSample.inSampleSize = getClosestResampleSize(bfo.outWidth, bfo.outHeight, maxDim); 

     Bitmap bmpt = BitmapFactory.decodeFile(path, optsDownSample); 

     Matrix m = new Matrix(); 

     if (bmpt.getWidth() > maxDim || bmpt.getHeight() > maxDim) 
     {   
      BitmapFactory.Options optsScale = getResampling(bmpt.getWidth(), bmpt.getHeight(), maxDim); 
      m.postScale((float)optsScale.outWidth/(float)bmpt.getWidth(), (float)optsScale.outHeight/(float)bmpt.getHeight()); 
      } 

     int sdk = new Integer(Build.VERSION.SDK).intValue(); 
     if (sdk > 4) 
     { 
      int rotation = ExifUtils.getExifRotation(path); 
      if (rotation != 0) 
      { 
       m.postRotate(rotation); 
      } 
     } 

     return Bitmap.createBitmap(bmpt, 0, 0, bmpt.getWidth(), bmpt.getHeight(), m, true); 
    } 

    private static BitmapFactory.Options getResampling(int cx, int cy, int max) 
    { 
     float scaleVal = 1.0f; 
     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     if (cx > cy) 
     { 
      scaleVal = (float)max/(float)cx; 
     } 
     else if (cy > cx) 
     { 
      scaleVal = (float)max/(float)cy; 
     } 
     else 
     { 
      scaleVal = (float)max/(float)cx; 
     } 
     bfo.outWidth = (int)(cx * scaleVal + 0.5f); 
     bfo.outHeight = (int)(cy * scaleVal + 0.5f); 
     return bfo; 
    } 

    private static int getClosestResampleSize(int cx, int cy, int maxDim) 
    { 
     /*Log.e("cx",""+cx); 
     Log.e("cy",""+cy);*/ 
     int max = Math.max(cx, cy); 

     int resample = 1; 
     for (resample = 1; resample < Integer.MAX_VALUE; resample++) 
     { 
      if (resample * maxDim > max) 
      { 
       resample--; 
       break; 
      } 
     } 

     if (resample > 0) 
     { 
      return resample; 
     } 
     return 1; 
    } 

    public static BitmapFactory.Options getBitmapDims(String path) throws Exception 
    { 
     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
     bfo.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, bfo); 
     return bfo; 
    } 
} 

다음은 Exif 클래스입니다.

public class ExifUtils 
{ 
    private ExifUtils() 
    { 
    } 

    public static int getExifRotation(String imgPath) 
    { 
     try 
     { 
      ExifInterface exif = new ExifInterface(imgPath); 
      String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
      if (!TextUtils.isEmpty(rotationAmount)) 
      { 
       int rotationParam = Integer.parseInt(rotationAmount); 
       switch (rotationParam) 
       { 
        case ExifInterface.ORIENTATION_NORMAL: 
         return 0; 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         return 90; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         return 180; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         return 270; 
        default: 
         return 0; 
       } 
      } 
      else 
      { 
       return 0; 
      } 
     } 
     catch (Exception ex) 
     { 
      return 0; 
     } 
    } 
} 

갤러리에서 선택한 이미지가 세로 또는 가로 유형인지 여부를 확인하고 sdcard의 새 경로에 저장 및 저장했습니다. OOM 문제를 피하려면 크기가 조정되었습니다.

+0

하지만 카메라에서 캡처하고 이미지를 사용하는 동안 누군가가 pls를 여기에 게시한다는 것을 알게되면이 기능이 작동하지 않습니다 ... –

관련 문제