사진 처리 상용 방법 총결

4705 단어 Android
1. 가우스 모드 처리
필기'고스 모드'부분을 조사하여 전문적으로 총결하였다.
2. 그림을 재단한다(화면 비율에 따라)
다음 방법은 전송된bitmap (처음에 이 방법을 썼을 때 시스템 벽지를 얻었을 때) 화면 크기에 따라 재단하는 것입니다.
public Bitmap setAndcropWallpaper(Context context, Bitmap wallpaper) {
	Bitmap targetBitmap = null;
	if (wallpaper != null) {
		try {
			int wallpaperWidth = wallpaper.getWidth();
			int wallpaperHeight = wallpaper.getHeight();
			DisplayMetrics dm = new DisplayMetrics();
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			wm.getDefaultDisplay().getMetrics(dm);
			int screenWidth = dm.widthPixels;// 1080
			int screenHeight = dm.heightPixels;// 1920
			float defaultScreen = (float) screenHeight / (float) screenWidth;
			Log.d(TAG, "defaultScreen=" + defaultScreen);
			if (defaultScreen < 1) {
				defaultScreen = (float) screenWidth / (float) screenHeight;
			}
			float bitmapSize = (float) wallpaperHeight / (float) wallpaperWidth;
			int disWidth = 0;
			int disHeight = 0;
			int captureBitmapWidth = wallpaperWidth;
			int captureBitmapHeight = wallpaperHeight;
			Log.d(TAG, "defaultScreen=" + defaultScreen + " bitmapSize=" + bitmapSize);
			if (defaultScreen > bitmapSize) { // bitmapHeight is min , capture bitmapWidth
				captureBitmapWidth = (int) ((screenWidth * wallpaperHeight) / screenHeight);
				disWidth = wallpaperWidth - captureBitmapWidth;
			} else if (defaultScreen < bitmapSize) {
				captureBitmapHeight = (int) ((wallpaperWidth * screenHeight) / screenWidth);
				disHeight = wallpaperHeight - captureBitmapHeight;
			}
			if (DEBUG) {
				Log.d(TAG, "setAndcropWallpaper ----- disWidth=" + disWidth + ",disHeight=" + disHeight
						+ ",captureBitmapWidth=" + captureBitmapWidth + ",captureBitmapHeight="
						+ captureBitmapHeight + " screenWidth=" + screenWidth + " screenHeight=" + screenHeight);
			}
			if (disWidth < 0 || disHeight < 0) {
				return null;
			}
			targetBitmap = Bitmap.createBitmap(wallpaper, disWidth / 2, disHeight / 2, captureBitmapWidth,
					captureBitmapHeight);
			float scaleWidth = (float) ((float) captureBitmapWidth / screenWidth);
			float scaleHeight = (float) ((float) captureBitmapHeight / screenHeight);
			Log.d(TAG, "scaleWidth=" + scaleWidth + " scaleHeight=" + scaleHeight);
			if ((targetBitmap != null) && (scaleWidth != 1 || scaleHeight != 1)) {
				Matrix matrix = new Matrix();
				/**
				 *                                ,       
				 */
				if (scaleWidth != 0.0f && scaleHeight != 0.0f && (scaleWidth != 1.0f || scaleHeight != 1.0f)) {//
					scaleWidth = 1.0f / scaleWidth;
					scaleHeight = 1.0f / scaleHeight;
					Log.d(TAG, "---scaleWidth=" + scaleWidth + " scaleHeight=" + scaleHeight);

				}
				matrix.postScale(scaleWidth, scaleHeight);
				int width = targetBitmap.getWidth();
				int height = targetBitmap.getHeight();
				Log.d(TAG, "---width=" + width + " height=" + height);
				targetBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, screenWidth > width ? width : screenWidth,
						screenHeight > height ? height : screenHeight, matrix, true);
			}
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
	}
	return targetBitmap;
}

3. 그림을 원형으로 재단
public static Bitmap cropBitmap(Bitmap bitmap) {//          
	int w = bitmap.getWidth(); //       , 
	int h = bitmap.getHeight();
	int cropWidth = w >= h ? h : w;//              

	return Bitmap.createBitmap(bitmap, (bitmap.getWidth() - cropWidth) / 2,
			(bitmap.getHeight() - cropWidth) / 2, cropWidth, cropWidth);
}

public static Bitmap getCircleBitmap(Bitmap bitmap) {//        
	if (bitmap == null) {
		return null;
	}
	bitmap = cropBitmap(bitmap);//      
	try {
		Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(circleBitmap);
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight());
		final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight()));
		float roundPx = 0.0f;
		roundPx = bitmap.getWidth();
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(Color.WHITE);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		final Rect src = new Rect(0, 0, bitmap.getWidth(),
				bitmap.getHeight());
		canvas.drawBitmap(bitmap, src, rect, paint);
		return circleBitmap;
	} catch (Exception e) {
		return bitmap;
	}
}

 
 
 
 

좋은 웹페이지 즐겨찾기