【问题标题】:Combining two bitmap image (side by side)合并两个位图图像(并排)
【发布时间】:2020-09-26 02:09:00
【问题描述】:

任何人都可以帮助将两个位图图像组合成一个位图

在安卓中(并排)。

谢谢, 尤瓦拉杰

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用Canvas - 查看这篇文章:

    http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

    更新代码以并行执行:

    public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
        Bitmap cs = null; 
    
        int width, height = 0; 
    
        if(c.getWidth() > s.getWidth()) { 
          width = c.getWidth() + s.getWidth(); 
          height = c.getHeight(); 
        } else { 
          width = s.getWidth() + s.getWidth(); 
          height = c.getHeight(); 
        } 
    
        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    
        Canvas comboImage = new Canvas(cs); 
    
        comboImage.drawBitmap(c, 0f, 0f, null); 
        comboImage.drawBitmap(s, c.getWidth(), 0f, null); 
    
        // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
        /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 
    
        OutputStream os = null; 
        try { 
          os = new FileOutputStream(loc + tmpImg); 
          cs.compress(CompressFormat.PNG, 100, os); 
        } catch(IOException e) { 
          Log.e("combineImages", "problem combining images", e); 
        }*/ 
    
        return cs; 
      } 
    

    【讨论】:

    • 感谢 xil3。我试过了,但它会将两张图像组合在一起,一张在另一张下方。但我需要并排合并两个图像
    • 我用代码更新了答案,它应该可以并行工作。仅对原始版本进行了一些小修改。
    • 谁能上传完整的图像合并源代码?我急需..请任何人帮助提前谢谢。
    • 如何将第二张图片放在第一张图片的右上角?此代码将第二个图像放在第一个图像的右侧。
    • 根据我拒绝的编辑,这里的尺寸计算有点偏离。将图像并排放置时,宽度始终为 c+s,并且您从 c 和 s 中取较高的高度。这是该部分的正确代码: if (c.getHeight() > s.getHeight()) { width = c.getWidth() + s.getWidth();高度 = c.getHeight(); } 其他 { 宽度 = c.getWidth() + s.getWidth();高度 = s.getHeight(); }
    【解决方案2】:

    我最终将 xil3 的答案修改为 kotlin 扩展函数,也许它对某人有用。就我而言,我希望图像垂直堆叠

    fun Bitmap?.combine(b: Bitmap?): Bitmap? =
    when {
        b == null || this == null -> {
            this ?: b
        } else -> {
            val cs = Bitmap.createBitmap(
                max(this.width, b.width),
                this.height + b.height,
                Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(cs)
            canvas.drawBitmap(this, 0f, 0f, null)
            canvas.drawBitmap(b, 0f, this.height.toFloat(), null)
            cs
        }
    }
    

    【讨论】:

      【解决方案3】:

      出色的工作选择的答案。 如果您想使用位图数组列表并并排查看以下内容:

      private Bitmap combineImageIntoOneFlexWidth(ArrayList<Bitmap> bitmap) {
              int w = 0, h = 0;
              for (int i = 0; i < bitmap.size(); i++) {
                  if (i < bitmap.size() - 1) {
                      h = bitmap.get(i).getHeight() > bitmap.get(i + 1).getHeight() ? bitmap.get(i).getHeight() : bitmap.get(i + 1).getHeight();
                  }
                  w += bitmap.get(i).getWidth();
              }
      
              Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(temp);
              int top = 0;
              for (int i = 0; i < bitmap.size(); i++) {
                  Log.e("HTML", "Combine: " + i + "/" + bitmap.size() + 1);
      
                  top = (i == 0 ? 0 : top + bitmap.get(i).getWidth());
                  //attributes 1:bitmap,2:width that starts drawing,3:height that starts drawing
                  canvas.drawBitmap(bitmap.get(i), top, 0f, null);
              }
              return temp;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-20
        • 2022-01-22
        • 2012-12-25
        • 1970-01-01
        • 2016-04-15
        • 2014-01-11
        • 1970-01-01
        相关资源
        最近更新 更多