当前位置:   article > 正文

安卓将图片分割或者拉伸或者旋转到指定尺寸并保存到本地_android bitmap 修改图片尺寸 拉伸

android bitmap 修改图片尺寸 拉伸

直接上代码吧:你们要用的话可以按照想法改

  1. package com.demo.util;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Matrix;
  5. import android.os.Environment;
  6. import android.util.Log;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. public class ImageProcessingUtils {
  11. // 将图片分割并拉伸到指定尺寸并保存到本地
  12. public static void splitAndSaveImage(String imagePath, int splitCount, int targetWidth, int targetHeight) {
  13. // 1. 读取原始图片
  14. Log.e("TAG", "imagePath=======" + imagePath);
  15. Bitmap originalBitmap = BitmapFactory.decodeFile(imagePath);
  16. try {
  17. int originalWidth = originalBitmap.getWidth();
  18. int originalHeight = originalBitmap.getHeight();
  19. Log.e("TAG", "originalWidth=======" + originalWidth);
  20. Log.e("TAG", "originalHeight=======" + originalHeight);
  21. // 2. 计算分割后每部分图片的宽度和高度
  22. int splitWidth = originalWidth / splitCount;
  23. int splitHeight = originalHeight - 1;
  24. Log.e("TAG", "splitWidth=======" + splitWidth);
  25. Log.e("TAG", "splitHeight=======" + splitHeight);
  26. // 3. 创建输出目录(如果不存在)
  27. File outputDirectory = new File(Environment.getExternalStorageDirectory() + "/split_images");
  28. if (!outputDirectory.exists()) {
  29. outputDirectory.mkdirs();
  30. }
  31. // 4. 分割并拉伸图片,并保存到本地
  32. for (int col = 0; col < splitCount; col++) {
  33. Log.e("TAG", "col=======" + col);
  34. int startX = col * splitWidth;
  35. int startY = 0;
  36. Log.e("TAG", "startX=======" + startX);
  37. Log.e("TAG", "startY=======" + startY);
  38. // 获取分割区域的图像
  39. Bitmap splitBitmap = Bitmap.createBitmap(originalBitmap, startX, startY,
  40. splitWidth, splitHeight);
  41. // 拉伸图像尺寸
  42. //Bitmap resizedBitmap = Bitmap.createScaledBitmap(splitBitmap,
  43. // targetWidth, targetHeight, true);
  44. // // 旋转图像
  45. // Matrix matrix = new Matrix();
  46. // matrix.postRotate(90);
  47. // Bitmap rotatedBitmap = Bitmap.createBitmap(splitBitmap, 0, 0,
  48. // splitWidth, splitHeight, matrix, true);
  49. //
  50. // 构建输出文件路径
  51. String outputFilePath = outputDirectory.getAbsolutePath() + "/split_image_" + col + ".png";
  52. Log.e("TAG", "outputFilePath=======" + outputFilePath);
  53. // 保存图像到本地
  54. saveImage(splitBitmap, outputFilePath);
  55. }
  56. }catch (Exception c){
  57. Log.e("TAG", "c=======" + c.getMessage());
  58. }
  59. }
  60. // 保存图片到本地
  61. private static void saveImage(Bitmap bitmap, String filePath) {
  62. try {
  63. FileOutputStream out = new FileOutputStream(filePath);
  64. bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  65. out.flush();
  66. out.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }

调用方式:ImageProcessingUtils.splitAndSaveImage(sourcePath,3,width,height);
 sourcePath:原始图像地址

3:平均分成三份

width,height:想要拉伸的尺寸

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/182019
推荐阅读
相关标签
  

闽ICP备14008679号