2014-11-07 2 views
3

외부 그림 디렉토리에 비트 맵을 저장하려고 할 때 문제가 있습니다. Bitmap.compress 함수를 사용하여 저장하면 비트 맵의 ​​투명도가 손실되고 배경이 검은 색이됩니다. 그러나 비트 맵을 이미지 뷰로 전달하고 액티비티에 표시하면 이미지가 잘 보이고 투명하게됩니다. 저장하려고 할 때만 투명성이 검은 색으로 변합니다.비트 맵 저장시 투명도가 손실됩니다.

나는 두 비트 맵과 포터 듀플 모드를 사용하여 비트 맵에 경로를 그리고 그려진 경로의 그림 만 표시하고 다른 모든 픽셀은 잘 리거나 투명해야한다고 말해야합니다.

그래서 여기에서는 경로 비트 맵을 생성하기위한 함수이다 :

private void createPathBitmap(RectF rect, Bitmap bitmap, Path path) { 
    RectF tmpRect = new RectF(rect); 
    Bitmap src = Bitmap.createBitmap(bitmap, (int) tmpRect.left, (int) tmpRect.top, (int) tmpRect.width(), (int) tmpRect.height()); 
    Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(dst); 

    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeCap(Paint.Cap.ROUND); 
    paint.setStrokeJoin(Paint.Join.ROUND); 
    paint.setStrokeWidth(mDisplayDensity * SnippetLayer.PATH_DIAMETER); 
    path.offset(-rect.left, -rect.top); 
    canvas.drawPath(path, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    RectF srcRect = new RectF(0, 0, rect.width(), rect.height()); 
    canvas.drawBitmap(src, null, srcRect, paint); 
    BitmapManager.sBitmapSnippet = dst; 
} 

하고 여기에 외부 저장하는 비트 맵을 저장하는 방법이다 : 사진 경로 만에 도시

SimpleDateFormat dateFormat = new SimpleDateFormat("HH_mm_ss_dd_MM_yyyy"); 
     File snippetFile = new File(picDir, fileName+"_"+dateFormat.format(new Date())+".png"); 
     try { 
      FileOutputStream fileOutputStream = new FileOutputStream(snippetFile); 
      BitmapManager.sBitmapSnippet.setHasAlpha(true); 
      BitmapManager.sBitmapSnippet.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); 
      fileOutputStream.flush(); 
      fileOutputStream.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

및 바운딩 박스의 나머지는 검은 색이고 투명하지 않습니다. 모든 도움에 감사드립니다.

+0

같은 문제가 발생합니다. 그걸 해결 했니? – Tooto

+0

안녕하세요, 불행히도 아니지만, 내 PC에서 이미지를 복사하고 이미지 뷰어 프로그램을 열 때 그 이미지가 투명한 배경을 가지고 있다는 것을 알아 냈습니다 ... 아마도 Android의 사진 앱을 사용하면 배경이 검은 색이됩니다 ... – sNore

+0

@sNore 이봐, 나는 같은 문제를 겪고있다. 너 해결 했니? –

답변

1

투명도가 떨어지는 이유는 정확히 모르겠지만 같은 문제가있었습니다. BitmapManager.sBitmapSnippet.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);BitmapManager.sBitmapSnippet.compress(Bitmap.CompressFormat.PNG, 0, fileOutputStream);으로 변경하기 만하면됩니다. 이렇게하면 전체 품질로 비트 맵이 압축되지만 투명 영역이 포함됩니다.

+0

고마워, 내가 시도 할거야 – sNore

4

I 출력 스트림으로 비트 맵 작성 압축() 메소드를 사용하고 :

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 

PNG 포맷을 사용하는 것이 중요하다. JPEG는 투명한 배경을 검은 색으로 변환합니다.

+0

이것은 eclips에서 작동하지만 안드로이드 스튜디오는 투명성을 잃을 수도있다 –

+0

고마워요 – Nhan

관련 문제