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;
	}
相关推荐
码农的小菜园1 小时前
Android的Locale学习笔记
android·笔记·学习
帅次1 小时前
链路到端上:HTTPS 之后安全题还在考什么
android·okhttp·glide·zygote·retrofit
游戏开发爱好者81 小时前
深入理解iOSTime Profiler:提升iOS应用性能的关键工具
android·ios·小程序·https·uni-app·iphone·webview
帅次2 小时前
Android 高级工程师面试参考答案:架构设计、Jetpack 与 Compose
android·面试·职场和发展·架构·composer·jetpack
limingade2 小时前
Dialer3.0智能拨号器Android版功能说明书
android·蓝牙电话·手机转sip·手机蓝牙·智能拨号器
JJay.2 小时前
Android BLE 的 notify 和 indicate 到底有什么区别
android
橙子199110162 小时前
Android 异步任务和消息机制
android
被开发耽误的大厨3 小时前
5、Integer缓存池里同一个对象指的是什么?Integer 和String 内存结构逻辑完全一样?
android·java·哈希算法
NoSi EFUL11 小时前
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
android·数据库·mysql
安小牛13 小时前
Android 开发汉字转带声调的拼音
android·java·学习·android studio