• 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 allbitmap =  new ArrayList();;
          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();

    1. public static void saveImage(String path, Bitmap bitmap){
    2. try {
    3. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
    4. bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
    5. bos.flush();
    6. bos.close();
    7. } catch (FileNotFoundException e) {
    8. e.printStackTrace();
    9. } catch (IOException e) {
    10. e.printStackTrace();
    11. }
    12. }
    13. public static Bitmap openImage(String path){
    14. Bitmap bitmap = null;
    15. try {
    16. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
    17. bitmap = BitmapFactory.decodeStream(bis);
    18. bis.close();
    19. } catch (FileNotFoundException e) {
    20. e.printStackTrace();
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. return bitmap;
    25. }
    26. public Bitmap mergeImages(List<Bitmap> images) {
    27. int maxWidth = 0;
    28. int totalHeight = 0;
    29. // 计算合并后图像的宽度和高度
    30. for (Bitmap image : images) {
    31. maxWidth = Math.max(maxWidth, image.getWidth());
    32. totalHeight += image.getHeight();
    33. }
    34. Log.e("log","maxWidth"+maxWidth + "totalHeight"+totalHeight);
    35. // 创建一个空白的Bitmap,用于绘制合并后的图像
    36. Bitmap result = Bitmap.createBitmap(maxWidth, totalHeight, Bitmap.Config.ARGB_8888);
    37. // 创建一个Canvas对象,并将其绑定到result Bitmap上
    38. Canvas canvas = new Canvas(result);
    39. int currentHeight = 0;
    40. // 将每个图像绘制到Canvas上
    41. for (Bitmap image : images) {
    42. canvas.drawBitmap(image, 0, currentHeight, null);
    43. currentHeight += image.getHeight();
    44. }
    45. return result;
    46. }

  • 相关阅读:
    Power Automate-输入数据
    JSP学生成长档案管理系统myeclipse开发sql数据库BS模式java编程网页结构
    4.5每日一题(多元函数比较大小通过偏积分)
    交互与前端18 CodeMirror 实践2
    【JavaEE---复习】四、事务
    带你熟练使用list
    ESP32 ADC测量电压 arduino
    DID:零知识证明的第一个应用试验场
    基于PYTHON游乐场服务管理系统的设计与实现
    torch中tensor的相关操作
  • 原文地址:https://blog.csdn.net/u011636207/article/details/132919001