图片处理常用方法总结

来源:互联网 发布:flotherm软件英文翻译 编辑:程序博客网 时间:2024/05/21 11:17

1. 高斯模式处理

查考笔记"高斯模式"部分,有专门总结

2.裁剪图片(按屏幕比例)

下面这个方法是将传入的bitmap(当初写这个方法的时候是取得系统壁纸)根据屏幕尺寸来裁剪
  1. public Bitmap setAndcropWallpaper(Context context,Bitmap wallpaper) {
  2. Bitmap targetBitmap = null;
  3. if (wallpaper != null) {
  4. try {
  5. int wallpaperWidth = wallpaper.getWidth();
  6. int wallpaperHeight = wallpaper.getHeight();
  7. DisplayMetrics dm = new DisplayMetrics();
  8. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  9. wm.getDefaultDisplay().getMetrics(dm);
  10. int screenWidth = dm.widthPixels;// 1080
  11. int screenHeight = dm.heightPixels;// 1920
  12. float defaultScreen = (float)screenHeight / (float)screenWidth;
  13. Log.d(TAG, "defaultScreen=" + defaultScreen);
  14. if (defaultScreen < 1) {
  15. defaultScreen = (float)screenWidth / (float)screenHeight;
  16. }
  17. float bitmapSize = (float)wallpaperHeight /(float)wallpaperWidth;
  18. int disWidth = 0;
  19. int disHeight = 0;
  20. int captureBitmapWidth = wallpaperWidth;
  21. int captureBitmapHeight = wallpaperHeight;
  22. Log.d(TAG, "defaultScreen=" + defaultScreen + " bitmapSize="
  23. + bitmapSize);
  24. if (defaultScreen > bitmapSize) { // bitmapHeight is min , capture bitmapWidth
  25. captureBitmapWidth = (int) ((screenWidth * wallpaperHeight) / screenHeight);
  26. disWidth = wallpaperWidth - captureBitmapWidth;
  27. } else if (defaultScreen < bitmapSize) {
  28. captureBitmapHeight = (int) ((wallpaperWidth * screenHeight) / screenWidth);
  29. disHeight = wallpaperHeight - captureBitmapHeight;
  30. }
  31. if (DEBUG) {
  32. Log.d(TAG, "setAndcropWallpaper ----- disWidth="
  33. + disWidth + ",disHeight=" + disHeight
  34. + ",captureBitmapWidth=" + captureBitmapWidth
  35. + ",captureBitmapHeight=" + captureBitmapHeight
  36. + " screenWidth=" + screenWidth + " screenHeight="
  37. + screenHeight);
  38. }
  39. if (disWidth < 0 || disHeight < 0) {
  40. return null;
  41. }
  42. targetBitmap = Bitmap.createBitmap(wallpaper, disWidth / 2,
  43. disHeight / 2, captureBitmapWidth, captureBitmapHeight);
  44. float scaleWidth = (float) ((float) captureBitmapWidth / screenWidth);
  45. float scaleHeight = (float) ((float) captureBitmapHeight / screenHeight);
  46. Log.d(TAG, "scaleWidth=" + scaleWidth + " scaleHeight="
  47. + scaleHeight);
  48. if ((targetBitmap != null)
  49. && (scaleWidth != 1 || scaleHeight != 1)) {
  50. Matrix matrix = new Matrix();
  51. /**
  52. * 处理之后的图片小于屏幕尺寸 处理原始的图片比例和屏幕比例是一致,但尺寸比屏幕大
  53. */
  54. if (scaleWidth != 0.0f && scaleHeight != 0.0f
  55. && (scaleWidth != 1.0f || scaleHeight != 1.0f)) {//
  56. scaleWidth = 1.0f / scaleWidth;
  57. scaleHeight = 1.0f / scaleHeight;
  58. Log.d(TAG, "---scaleWidth=" + scaleWidth + " scaleHeight=" + scaleHeight);
  59. }
  60. matrix.postScale(scaleWidth, scaleHeight);
  61. int width = targetBitmap.getWidth();
  62. int height = targetBitmap.getHeight();
  63. Log.d(TAG, "---width=" + width + " height=" + height);
  64. targetBitmap = Bitmap.createBitmap(targetBitmap, 0, 0,
  65. screenWidth > width ? width : screenWidth,
  66. screenHeight > height ? height : screenHeight,
  67. matrix, true);
  68. }
  69. } catch (OutOfMemoryError e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. return targetBitmap;
  74. }






0 0
原创粉丝点击