2016-07-01 6 views
1

회전 된 이미지를 회전하여 저장하고 안드로이드 장치 내의 다른 위치로 옮기고 싶습니다.비트 맵 회전 및 저장 이미지

  1. 이미지를 회전하여 이미지보기로 설정할 수 있습니다.
  2. 유엔 - 회전 이미지를 내가 선택한 목적지로 복사 할 수 있습니다.

내가 저장 한 회전 된 이미지 파일을 얻을 수 있습니다 할 수없는 나는 유일한 (rotated.jpg)

아래 내 코드 회전 (이 스토리지로 회전 된 파일을 저장하지 않습니다?)

   Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 

       Matrix matrix = new Matrix(); 
       matrix.postRotate(getImageOrientation(filePathLocal)); 
       rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);   

       //Set Image 
       ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile); 
       ibProfile.setImageBitmap(rotatedBitmap); 

이제 위 내용은 활동이 끝날 때까지 일시적으로 회전합니다. 이제 위의 코드에서 회전 된 이미지를 저장하고 서버에 업로드하기 전에 somehwere로 이동하고 싶습니다. 이미 파일 복사/이동 및 업로드 방법을 알고 있습니다. 그 코드를 게시 할 필요가 없습니다 - 필요한 모든 것은 회전 된 이미지를 저장하는 코드이므로 제가 가지고있는 것입니다. /sdcard/saved_rotated_image.jpg처럼 오링 createbitmap의 마지막 인수에 false를 넣어

+0

파일에 비트 맵을 저장하는 방법을 알고 있습니까? –

답변

2
Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 

      Matrix matrix = new Matrix(); 
      matrix.postRotate(getImageOrientation(filePathLocal)); 
      rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);   

      //Set Image 
      ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile); 
      ibProfile.setImageBitmap(rotatedBitmap); 

]을 참조 내 코드 :

@Override 
    protected Uri doInBackground(String... params) { 
     String filepath = params[0]; 
     String filename = params[1]; 
     String filetype = params[2]; 

     Bitmap bitmap = takeScreenShot(root); 
     Matrix rotateMatrix = new Matrix(); 
     rotateMatrix.postRotate(Datas.rotationvalue); 
     Log.e("rotationvalue", Datas.rotationvalue+"..."); 
     Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, false); 

     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes); 
     try { 
      File f = new File(filepath); 
      if (!f.exists()) { 
       f.mkdir(); 
      } 
      String folderpath = f.toString(); 
      File file = new File(folderpath, filename + "." + filetype); 

      file.createNewFile(); 
      FileOutputStream fo = new FileOutputStream(file); 
      fo.write(bytes.toByteArray()); 
      fo.close(); 
      Uri uri = Uri.fromFile(file); 
      Log.e("Edited img uri", uri.toString()); 
      return uri; 
     } catch (Exception e) { 
      Log.e("Exception...occured", e.toString()); 
      e.printStackTrace(); 
      return null; 
     } 
    } 

이 코드는 내 자신의 기 전 노력하고 있습니다 옆에서 시도하십시오.

에 따르면 doc : last 인수는 필터 부울입니다. 소스 을 필터링해야하는 경우 true입니다. 행렬에 이상의 번역이 포함 된 경우에만 적용됩니다. 추가 정보를

링크 : Last argument more info

+0

감사합니다. Bitmap.CompressFormat.PNG를 Bitmap.CompressFormat.JPEG로 변경해야했습니다. – user3560827

+0

그래요. 그렇다면이 정보가 더 많은 정보를 얻고, 친절하게 upvote하면 –

+0

승인되고 upvoted, 네트워크 여야합니다. 간섭, 다시 할께. – user3560827

1

저장 이미지의 기능은 다음과 같다 :

\t private void saveBitmap(Bitmap bitmap, String fileName) { 
 
\t \t File file = new File(Environment.getExternalStorageDirectory(), fileName); 
 
\t \t FileOutputStream fileOS = null; 
 
\t \t try { 
 
\t \t \t fileOS = new FileOutputStream(file); 
 
\t \t \t // quality: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. 
 
\t \t \t bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS); 
 
\t \t \t fileOS.flush(); 
 
\t \t } catch (IOException e) { 
 
\t \t \t e.printStackTrace(); 
 
\t \t } finally { 
 
\t \t \t if (fileOS != null) { 
 
\t \t \t \t try { 
 
\t \t \t \t \t fileOS.close(); 
 
\t \t \t \t } catch (IOException e) { 
 
\t \t \t \t \t e.printStackTrace(); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t }

사용 예제 :

Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 
 

 
    Matrix matrix = new Matrix(); 
 
    matrix.postRotate(getImageOrientation(filePathLocal)); 
 
    rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);   
 

 
    saveBitmap(rotatedBitmap, "saved_rotated_image.jpg");