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;
	}
相关推荐
雨白9 小时前
装饰设计模式:从咖啡加料到 Android 图片加载实战
android
传奇开心果编程14 小时前
【六大移动UI框架比较学与练】第3课:文本、图片、布局核心组件实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
墨狂之逸才15 小时前
键盘一弹,扫码枪就失灵:Android 硬件扫码与软键盘冲突的根因与解法
android
妖星19 小时前
Android 13 WindowContainer 树是如何一步一步创建出来的
android
zhangphil20 小时前
Android产品级图库/相册应用关键性能瓶颈:媒体数据聚合
android
StarsGreen21 小时前
Android系统相关命令介绍
android·windows
传奇开心果编程21 小时前
【六大移动UI框架比较学与练】第4课:列表与滚动——懒加载、复用与状态保持实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
恋猫de小郭21 小时前
Flutter 奇葩 Bug:错过一次合成,后续每帧的 Raster 都顺带变慢?
android·前端·flutter
三少爷的鞋1 天前
一段 Kotlin 协程代码,藏着 5 个问题:你能发现几个?
android
棒棒的唐1 天前
发一个用本地AI(openclaw)写的调用本地llama.cpp服务对话,并按指定要求识别图片的PHP类
android·php·llama