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


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

    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:想要拉伸的尺寸

  • 相关阅读:
    每日汇评:黄金形态确认牛市,再次尝试上行2000美元
    ant.design 的 Pro Component 的 ProTable 清除表单内容的方法
    Python从基础到项目实战——基础篇
    全栈开发可能需要的环境及工具
    fastjson 1.2.24 反序列化导致任意命令执行漏洞
    《用Go语言自制解释器》之第5章 宏系统
    【java期末复习题】第10章 Java输入与输出
    推荐系统实现原理介绍
    MySQL安装教程-手把手教你安装
    【C++】多线程的学习笔记(2)——白话文版(bushi
  • 原文地址:https://blog.csdn.net/qq_36333309/article/details/133125520