android 多张图片合成一张

复制代码
new Thread(new Runnable() {
   @Override
   public void run() {
      Long a =  System.currentTimeMillis();
      File file1 = new File(div,  "1694834497111"+".jpg");
      File file2 = new File(div,  "1694834502113"+".jpg");
      File file3 = new File(div,  "1694835780761"+".jpg");
   
 
      Bitmap bitmap1 = openImage(file1.getAbsolutePath());
      Bitmap bitmap2 = openImage(file2.getAbsolutePath());
      Bitmap bitmap3 = openImage(file3.getAbsolutePath());
      List<Bitmap> allbitmap =  new ArrayList<Bitmap>();;
      allbitmap.add(bitmap1);
      allbitmap.add(bitmap2);
      allbitmap.add(bitmap3);
      Bitmap big = mergeImages(allbitmap);
      String path = div.getAbsolutePath()+"/ABC.jpg";
      Log.e("log","path"+path);
      saveImage(path,big);
      Log.e("log","time:::" + (System.currentTimeMillis()-a));

   }
}).start();
复制代码
	public static void saveImage(String path, Bitmap bitmap){
		try {
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
			bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
			bos.flush();
			bos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Bitmap openImage(String path){
		Bitmap bitmap = null;
		try {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
			bitmap = BitmapFactory.decodeStream(bis);
			bis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}


	public Bitmap mergeImages(List<Bitmap> images) {
		int maxWidth = 0;
		int totalHeight = 0;

		// 计算合并后图像的宽度和高度
		for (Bitmap image : images) {
			maxWidth = Math.max(maxWidth, image.getWidth());
			totalHeight += image.getHeight();
		}
		Log.e("log","maxWidth"+maxWidth  + "totalHeight"+totalHeight);
		// 创建一个空白的Bitmap,用于绘制合并后的图像
		Bitmap result = Bitmap.createBitmap(maxWidth, totalHeight, Bitmap.Config.ARGB_8888);

		// 创建一个Canvas对象,并将其绑定到result Bitmap上
		Canvas canvas = new Canvas(result);

		int currentHeight = 0;

		// 将每个图像绘制到Canvas上
		for (Bitmap image : images) {
			canvas.drawBitmap(image, 0, currentHeight, null);
			currentHeight += image.getHeight();
		}

		return result;
	}
相关推荐
半摆烂日常4 小时前
低代码平台API对接实践:接口鉴权与数据同步的完整实现
android·低代码·rxjava
事圆则缓4 小时前
volatile 使用场景全解析:它能保证什么,又解决不了什么
android
蒲公英内测分发5 小时前
智能投影仪参加海外展会,未上架的 Android 配套 App 怎么让客户限时试用?
android
ii_best5 小时前
按键精灵手机端开发安卓版实战:手写一个可最小化、可拖动的「运行日志悬浮窗」(附完整 源码 + 踩坑记录)
android·ios·智能手机·自动化·ai编程·按键精灵
终端安全笔记5 小时前
安卓做 MDM 管控,先分清三种注册入口
android·安全·智能手机
乌萨达6 小时前
手机模拟器安卓怎么用?电脑安装应用、玩手游与账号同步指南
android·智能手机·电脑·雷电模拟器
美狐美颜SDK开放平台7 小时前
Android与iOS直播APP美颜有什么区别?视频美颜SDK开发详解
android·ios·音视频·视频美颜sdk
春猿火9 小时前
某市-2026【网安·论道】misc-afterimage_note-wp
android
恋猫de小郭10 小时前
Shopify 回应为什么从 RN 回到原生,为什么不用 KMP ?
android·前端·flutter
fundroid11 小时前
Android AI 开发,真正拉开差距的是工程闭环
android·ai·大模型·agent