비트 맵의 안드로이드 자르기 센터
정사각형 또는 직사각형 비트 맵이 있습니다. 나는 가장 짧은면을 가지고 다음과 같이합니다.
int value = 0;
if (bitmap.getHeight() <= bitmap.getWidth()) {
value = bitmap.getHeight();
} else {
value = bitmap.getWidth();
}
Bitmap finalBitmap = null;
finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value);
그런 다음 이것을 사용하여 144 x 144 비트 맵으로 크기를 조정합니다.
Bitmap lastBitmap = null;
lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);
문제는 원본 비트 맵의 왼쪽 상단을 자르는 것입니다. 비트 맵의 중심을 자르는 코드가있는 사람이 있습니까?
이은 달성 될 수 Bitmap.createBitmap (소스, x, y, 폭, 높이)
if (srcBmp.getWidth() >= srcBmp.getHeight()){
dstBmp = Bitmap.createBitmap(
srcBmp,
srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
0,
srcBmp.getHeight(),
srcBmp.getHeight()
);
}else{
dstBmp = Bitmap.createBitmap(
srcBmp,
0,
srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
srcBmp.getWidth(),
srcBmp.getWidth()
);
}
위의 답변 중 대부분 이이 작업을 수행하는 방법을 제공하지만 이미이 작업을 수행하는 기본 방법이 있으며 한 줄의 코드 ( ThumbnailUtils.extractThumbnail()
)입니다.
int dimension = getSquareCropDimensionForBitmap(bitmap);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
...
//I added this method because people keep asking how
//to calculate the dimensions of the bitmap...see comments below
public int getSquareCropDimensionForBitmap(Bitmap bitmap)
{
//use the smallest dimension of the image to crop to
return Math.min(bitmap.getWidth(), bitmap.getHeight());
}
비트 맵 객체를 재활용하려는 경우 다음과 같은 옵션을 전달할 수 있습니다.
bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
보낸 사람 : ThumbnailUtils 설명서
공개 정적 비트 맵 extractThumbnail (비트 맵 소스, int 너비, int 높이)
API 레벨 8에 추가됨 원하는 크기의 중앙 비트 맵을 만듭니다.
Parameters source original bitmap source width targeted width height targeted height
I was getting out of memory errors sometimes when using the accepted answer, and using ThumbnailUtils resolved those issues for me. Plus, this is much cleaner and more reusable.
Have you considered doing this from the layout.xml
? You could set for your ImageView
the ScaleType to android:scaleType="centerCrop"
and set the dimensions of the image in the ImageView
inside the layout.xml
.
You can used following code that can solve your problem.
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
여기 에 임의의 차원 의 [비트 맵] 의 중심을 잘라 내고 원하는 [IMAGE_SIZE]로 결과를 스케일링 하는보다 완전한 스 니펫이 있습니다 . 따라서 항상 고정 된 크기 의 [croppedBitmap] 스케일 사각형 이미지 센터를 얻게됩니다 . 썸네일 등에 이상적입니다.
다른 솔루션의 더 완벽한 조합입니다.
final int IMAGE_SIZE = 255;
boolean landscape = bitmap.getWidth() > bitmap.getHeight();
float scale_factor;
if (landscape) scale_factor = (float)IMAGE_SIZE / bitmap.getHeight();
else scale_factor = (float)IMAGE_SIZE / bitmap.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(scale_factor, scale_factor);
Bitmap croppedBitmap;
if (landscape){
int start = (tempBitmap.getWidth() - tempBitmap.getHeight()) / 2;
croppedBitmap = Bitmap.createBitmap(tempBitmap, start, 0, tempBitmap.getHeight(), tempBitmap.getHeight(), matrix, true);
} else {
int start = (tempBitmap.getHeight() - tempBitmap.getWidth()) / 2;
croppedBitmap = Bitmap.createBitmap(tempBitmap, 0, start, tempBitmap.getWidth(), tempBitmap.getWidth(), matrix, true);
}
아마도 가장 쉬운 해결책은 다음과 같습니다.
public static Bitmap cropCenter(Bitmap bmp) {
int dimension = Math.min(bmp.getWidth(), bmp.getHeight());
return ThumbnailUtils.extractThumbnail(bmp, dimension, dimension);
}
수입품 :
import android.media.ThumbnailUtils;
import java.lang.Math;
import android.graphics.Bitmap;
@willsteel 솔루션을 정정하려면 다음을 수행하십시오.
if (landscape){
int start = (tempBitmap.getWidth() - tempBitmap.getHeight()) / 2;
croppedBitmap = Bitmap.createBitmap(tempBitmap, start, 0, tempBitmap.getHeight(), tempBitmap.getHeight(), matrix, true);
} else {
int start = (tempBitmap.getHeight() - tempBitmap.getWidth()) / 2;
croppedBitmap = Bitmap.createBitmap(tempBitmap, 0, start, tempBitmap.getWidth(), tempBitmap.getWidth(), matrix, true);
}
public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
if (w == size && h == size) return bitmap;
// scale the image so that the shorter side equals to the target;
// the longer side will be center-cropped.
float scale = (float) size / Math.min(w, h);
Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
int width = Math.round(scale * bitmap.getWidth());
int height = Math.round(scale * bitmap.getHeight());
Canvas canvas = new Canvas(target);
canvas.translate((size - width) / 2f, (size - height) / 2f);
canvas.scale(scale, scale);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint);
if (recycle) bitmap.recycle();
return target;
}
private static Bitmap.Config getConfig(Bitmap bitmap) {
Bitmap.Config config = bitmap.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
return config;
}
public Bitmap getResizedBitmap(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
int narrowSize = Math.min(width, height);
int differ = (int)Math.abs((bm.getHeight() - bm.getWidth())/2.0f);
width = (width == narrowSize) ? 0 : differ;
height = (width == 0) ? differ : 0;
Bitmap resizedBitmap = Bitmap.createBitmap(bm, width, height, narrowSize, narrowSize);
bm.recycle();
return resizedBitmap;
}
참고 URL : https://stackoverflow.com/questions/6908604/android-crop-center-of-bitmap
'IT story' 카테고리의 다른 글
버튼을 클릭 할 때 전화 설정을 열려면 어떻게합니까? (0) | 2020.06.20 |
---|---|
문자열 객체 목록을 연결하는 가장 좋은 방법은 무엇입니까? (0) | 2020.06.20 |
첫 번째 응답이 AppCache (Symfony2)의 개인 응답이면 괜찮습니까? (0) | 2020.06.20 |
Chrome 및 Firefox에서 클립 보드 JavaScript 기능의 비밀 복사? (0) | 2020.06.20 |
Java8에서 람다를 사용하여 null이 아닌 경우에만 값 필터링 (0) | 2020.06.19 |