0

나는 이미지를 다운로드하고 picasso를 사용하여 CardView에 표시하는 서비스가 있습니다.이미지에서 공백 자르기

Picasso.with(context).load(beer.getLabelLarge()).fit().centerCrop().into(imgBeerLabel); 

그러나 이러한 이미지는 바람직하지 않은 공백으로 참여 제공 : 흰색 테두리가없이,

enter image description here

enter image description here

나는 이러한 이미지를 다듬을 만 라벨을 보여주고, ImageView 크기로 크기 조정.

- 편집

흰색 부분의 크기는 다양합니다.

https://stackoverflow.com/questions/12175991/crop-image-white-space-automatically-using-jquery 

그러나 거기는 내가이 시간에 방지하고자하는 많은 코드로 해결되었다 :

문제는이 질문에보고 된 동일합니다.

이미지에서 특정 사각형에 초점을 맞추고 해당 부분을 확대하여 흰색 부분의 크기에 관계없이 전체 화면을 채우는 데 도움이 될 수있는 가능한 솔루션은 다음과 같습니다.

결과 예상은 다음과 같습니다 피카소와 변환을 사용하여이 작업을 수행 할 수있는 방법이

enter image description here

있습니까? 벡터 이미지의

+0

레이블 너비와 높이를 알고 계십니까? 변환을 사용하여 필요한 크기로 자르기 – Raghunandan

+0

아마도 답변과 같은 것을 시도해 볼 수 있습니다. https://stackoverflow.com/questions/12175991/crop-image-white-space-automatically-using-jquery –

+0

@Raghunandan no. 변수입니다. – alexpfx

답변

1

나인 패치 NinePatch 이미지의 경우 코드는 this library에 있습니다.

양쪽의 첫 번째 다른 픽셀을 세로 및 가로로 찾고 비트 맵을 자릅니다. 그것은 완벽하지는 않지만 내 필요에 어울립니다.

public class CropMiddleFirstPixelTransformation implements Transformation { 
    private int mWidth; 
    private int mHeight; 


    @Override 
    public Bitmap transform(Bitmap source) { 
     int width = source.getWidth(); 
     int height = source.getHeight(); 

     int[] horizontalMiddleArray = new int[width]; 
     source.getPixels(horizontalMiddleArray, 0, width, 0, height/2, width, 1); 

     int[] verticalMiddleArray = new int[height]; 
     source.getPixels(verticalMiddleArray, 0, 1, width/2, 0, 1, height); 

     int left = getFirstNonWhitePosition(horizontalMiddleArray); 
     int right = getLastNonWhitePosition(horizontalMiddleArray); 

     int top = getFirstNonWhitePosition(verticalMiddleArray); 
     int bottom = getLastNonWhitePosition(verticalMiddleArray); 

     mWidth = right - left; 
     mHeight = bottom - top; 


     if (!isNegative(left, right, top, bottom)) { 
      return source; 
     } 

     Bitmap bitmap = Bitmap.createBitmap(source, left, top, mWidth , mHeight); 
     source.recycle(); 
     return bitmap; 

    } 

    private boolean isNegative(int... values) { 
     for (int i : values) { 
      if (i < 0) { 
       return false; 
      } 
     } 
     return true; 

    } 

    private int getFirstNonWhitePosition(int[] horizontalMiddleArray) { 
     int left = 0; 
     for (int i = 0; i < horizontalMiddleArray.length; i++) { 
      if (i == 0) { 
       left = horizontalMiddleArray[i]; 
      } 
      if (left != horizontalMiddleArray[i]) { 
       return i; 
      } 
     } 
     return -1; 
    } 

    private int getLastNonWhitePosition(int[] horizontalMiddleArray) { 
     int right = 0; 
     int length = horizontalMiddleArray.length; 
     for (int i = length - 1; i > 0; i--) { 
      if (i == length - 1) { 
       right = horizontalMiddleArray[i]; 
      } 
      if (right != horizontalMiddleArray[i]) { 
       return i; 
      } 
     } 
     return -1; 
    } 


    @Override 
    public String key() { 
     return "CropMiddleFirstPixelTransformation(width=" + mWidth + ", height=" + mHeight + ")"; 
    } 
}