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

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

复制代码
package com.demo.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageProcessingUtils {

    // 将图片分割并拉伸到指定尺寸并保存到本地
    public static void splitAndSaveImage(String imagePath, int splitCount, int targetWidth, int targetHeight) {
        // 1. 读取原始图片
        Log.e("TAG", "imagePath=======" + imagePath);
        Bitmap originalBitmap = BitmapFactory.decodeFile(imagePath);
        try {
            int originalWidth = originalBitmap.getWidth();
            int originalHeight = originalBitmap.getHeight();
            Log.e("TAG", "originalWidth=======" + originalWidth);
            Log.e("TAG", "originalHeight=======" + originalHeight);
            // 2. 计算分割后每部分图片的宽度和高度
            int splitWidth = originalWidth / splitCount;
            int splitHeight = originalHeight - 1;
            Log.e("TAG", "splitWidth=======" + splitWidth);
            Log.e("TAG", "splitHeight=======" + splitHeight);
            // 3. 创建输出目录(如果不存在)
            File outputDirectory = new File(Environment.getExternalStorageDirectory() + "/split_images");
            if (!outputDirectory.exists()) {
                outputDirectory.mkdirs();
            }

            // 4. 分割并拉伸图片,并保存到本地
            for (int col = 0; col < splitCount; col++) {
                Log.e("TAG", "col=======" + col);
                int startX = col * splitWidth;
                int startY = 0;
                Log.e("TAG", "startX=======" + startX);
                Log.e("TAG", "startY=======" + startY);
                // 获取分割区域的图像
                Bitmap splitBitmap = Bitmap.createBitmap(originalBitmap, startX, startY,
                        splitWidth, splitHeight);

                // 拉伸图像尺寸
               //Bitmap resizedBitmap = Bitmap.createScaledBitmap(splitBitmap,
               //        targetWidth, targetHeight, true);


               // // 旋转图像
               // Matrix matrix = new Matrix();
               // matrix.postRotate(90);
               // Bitmap rotatedBitmap = Bitmap.createBitmap(splitBitmap, 0, 0,
               //         splitWidth, splitHeight, matrix, true);
//
                // 构建输出文件路径
                String outputFilePath = outputDirectory.getAbsolutePath() + "/split_image_" + col + ".png";

                Log.e("TAG", "outputFilePath=======" + outputFilePath);
                // 保存图像到本地
                saveImage(splitBitmap, outputFilePath);
            }
        }catch (Exception c){
            Log.e("TAG", "c=======" + c.getMessage());
        }

    }

    // 保存图片到本地
    private static void saveImage(Bitmap bitmap, String filePath) {
        try {
            FileOutputStream out = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

调用方式:ImageProcessingUtils.splitAndSaveImage(sourcePath,3,width,height);

sourcePath:原始图像地址

3:平均分成三份

width,height:想要拉伸的尺寸

相关推荐
2501_9159184139 分钟前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者841 分钟前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview
雨白5 小时前
实现双向滑动的 ScalableImageView(上)
android
Y4090016 小时前
数据库基础知识——聚合函数、分组查询
android·数据库
没有了遇见11 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_9160088912 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214312 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO12 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO12 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
g_i_a_o_giao15 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析