Android 크롭 기능 없이 이미지 자르기
2020. 6. 29. 09:32ㆍAndroid
반응형
Uri이미지를 가운데를 기준으로 w, h 크기 만큼 crop한다.
public Bitmap cropCenterBitmap(Uri uri, int w, int h) {
Bitmap src = null;
try {
src = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(src == null)
return null;
int width = src.getWidth();
int height = src.getHeight();
if(width < w && height < h)
return src;
int x = 0;
int y = 0;
if(width > w){
x = (width - w)/2;
}
if(height > h){
y = (height - h) / 2;
}
int cw = w; // crop width
int ch = h; // crop height
if(w > width)
cw = width;
if(h > height)
ch = height;
return Bitmap.createBitmap(src, x, y, cw, ch);
}
반응형
'Android' 카테고리의 다른 글
Android Hilt 에 대해 정리 (0) | 2022.05.09 |
---|---|
Android Paging3 , Room , Flow 관련 백그라운드 쓰레드 (0) | 2022.05.04 |
Android 이미지 해상도별 자동 비율 구하기 (0) | 2020.06.29 |
[안드로이드] 각 프래그먼트 별 뒤로가기 버튼(백버튼) 이벤트 (뷰페이저 사용) (0) | 2020.06.15 |
Android ViewPager2 + TabView JAVA (0) | 2020.06.11 |