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;
	}
相关推荐
2501_9151063220 小时前
iOS 26 APP 性能测试实战攻略:多工具组合辅助方案
android·macos·ios·小程序·uni-app·cocoa·iphone
怪兽20141 天前
IntentService 的应用场景和使用方式?
android·面试
Jeled1 天前
云信im在Android中的使用2
android
Jerry1 天前
Compose 自定义布局和图形
android
杨筱毅1 天前
【Android】【底层机制】组件生命周期以及背后的状态管理
android·底层机制
Jeled1 天前
Kotlin 实现社交 App 音视频模块:语音录制、播放、暂停与进度控制全流程封装
android·kotlin·android studio·音视频
沐怡旸1 天前
【底层机制】【Android】Binder架构与原理
android·面试
Jeled1 天前
Jetpack —> Media3的分析和使用
android
木易士心1 天前
Android setContentView源码与原理分析
android
00后程序员张1 天前
iOS混淆与IPA文件加固全流程实战 防止苹果应用被反编译的工程级方案
android·ios·小程序·https·uni-app·iphone·webview