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;
	}
相关推荐
Android轮子哥10 分钟前
尝试解决 Android 适配最后一公里
android
雨白1 小时前
OkHttp 源码解析:enqueue 非同步流程与 Dispatcher 调度
android
风往哪边走2 小时前
自定义仿日历组件弹框
android
没有了遇见2 小时前
Android 外接 U 盘开发实战:从权限到文件复制
android
Monkey-旭3 小时前
Android 文件存储机制全解析
android·文件存储·kolin
zhangphil3 小时前
Android Coil 3拦截器Interceptor计算单次请求耗时,Kotlin
android·kotlin
DokiDoki之父4 小时前
多线程—飞机大战排行榜功能(2.0版本)
android·java·开发语言
用户2018792831676 小时前
强制关闭生命周期延时的Activity实现思路
android
用户2018792831676 小时前
Activity后生命周期暂停问题
android
用户2018792831676 小时前
浅析:WindowManager添加的 View 的事件传递机制
android