【问题标题】:combining two png files in android在android中组合两个png文件
【发布时间】:2010-04-29 16:03:15
【问题描述】:

我有两个 png 图像文件,我希望我的 android 应用程序以编程方式将它们组合成一个 png 图像文件,我想知道是否可以这样做?如果是这样,我想做的就是将它们相互叠加以创建一个文件。

这背后的想法是我有一些 png 文件,其中一些图像的一部分在左侧,其余部分透明,其他图像在右侧,其余部分透明。并根据用户输入将两者结合起来制作一个文件来显示。 (我不能只是并排显示两个图像,它们需要是一个文件)

这有可能在 android 中以编程方式实现吗?

【问题讨论】:

标签: android image


【解决方案1】:

我一直在尝试解决这个问题。

这是(基本上)我用来使它工作的代码。

// Get your images from their files
Bitmap bottomImage = BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = BitmapFactory.decodeFile("myOtherPNG.png");

// As described by Steve Pomeroy in a previous comment, 
// use the canvas to combine them.
// Start with the first in the constructor..
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);

// comboImage is now a composite of the two. 

// To write the file out to the SDCard:
OutputStream os = null;
try {
    os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
    comboImage.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
    e.printStackTrace();
}

编辑:

有一个错字, 所以,我改变了

image.compress(CompressFormat.PNG, 50, os)

bottomImage.compress(CompressFormat.PNG, 50, os)

【讨论】:

  • 这段代码中的图像是什么 image.compress(CompressFormat.PNG, 50, os) 请告诉我...
  • Lalit 我只是猜测 image.compress 是一个错字,它应该是 comboImage.compress
  • @Second 从BitmapFactory 中删除了new
  • 如果有人来到这里并且对他为什么调用bottomImage.compress感到困惑,当您使用位图实例化画布时,它将在原始位图上绘制。
【解决方案2】:

您可以进行混合。这并不是安卓特有的。这只是通用的图像处理。

编辑:

您可能会发现这些文章、示例和代码很有用:

http://www.jhlabs.com/ip/

http://kfb-android.blogspot.com/2009/04/image-processing-in-android.html

http://code.google.com/p/jjil/

Image Processing on Android

【讨论】:

  • 但 android 是否支持混合以及如何支持?
【解决方案3】:

我用这个代码

private class PhotoComposition extends AsyncTask<Object, Void, Boolean> {
    private String pathSave;//path save combined images

    @Override
    protected Boolean doInBackground(Object... objects) {

      List<String> images = (List<String>) objects[0]; //lsit of path iamges
      pathSave = (String) objects[1];//path save combined images
      if (images.size() == 0) {
        return false;
      }
      List<Bitmap> bitmaps = new ArrayList<>();
      for (int i = 0; i < images.size(); i++) {
        bitmaps.add(BitmapFactory.decodeFile( images.get(i)));
      }
      int width = findWidth(bitmaps);//Find the width of the composite image
      int height = findMaxHeight(bitmaps);//Find the height of the composite image

      Bitmap combineBitmap  = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//create bitmap of composite image

      combineBitmap.eraseColor(Color.parseColor("#00000000")); //bcakgraound color of composite image

      Bitmap mutableCombineBitmap = combineBitmap.copy(Bitmap.Config.ARGB_8888, true);//create mutable bitmap to create canvas

      Canvas canvas = new Canvas(mutableCombineBitmap);// create canvas to add bitmaps

      float left = 0f;

      for (int i = 0; i < bitmaps.size(); i++) {
        canvas.drawBitmap(bitmaps.get(i), left, 0f, null);//Taking photos horizontally

        left += bitmaps.get(i).getWidth();//Take right to the size of the previous photo
      }

      OutputStream outputStream = null;
      try {
        outputStream = new FileOutputStream(pathSave);//path of save composite image
        mutableCombineBitmap.compress(Bitmap.CompressFormat.PNG, 80, outputStream);
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      }
      return true;
    }


    @Override
    protected void onPostExecute(Boolean isSave) {
      if (isSave) {
        //iamge save on pathSave
        Log.i("PhotoComposition", "onPostExecute: " + pathSave);
      }
      super.onPostExecute(isSave);
    }

    private int findMaxHeight(List<Bitmap> bitmaps) {
      int maxHeight = Integer.MIN_VALUE;
      for (int i = 0; i < bitmaps.size(); i++) {
        if (bitmaps.get(i).getHeight() > maxHeight) {
          maxHeight = bitmaps.get(i).getHeight();
        }
      }
      return maxHeight;
    }

    private int findWidth(List<Bitmap> bitmaps) {
      int width = 0;
      for (int i = 0; i < bitmaps.size(); i++) {
        width += bitmaps.get(i).getWidth();
      }
      return width;
    }

用法

List<String> images = new ArrayList<>();
    images.add("/storage/emulated/0/imageOne.png");//path of image in storage
    images.add("/storage/emulated/0/imageTwo.png");
//   images.add("/storage/emulated/0/imageThree");
//   ... //add more images
    String pathSaveCombinedImage = "/storage/emulated/0/CombinedImage.png";//path save result image
  new PhotoComposition().execute(images, pathSaveCombinedImage);

而使用上述代码的结果如下

【讨论】:

    【解决方案4】:

    您可能希望查看Canvas 对象,这样可以轻松进行其他绘图操作。您可以将位图绘制到您想要的画布上,然后保存生成的位图。

    【讨论】:

      【解决方案5】:

      如果它们有透明部分,那么如果您在另一个之上绘制一个,则只有非透明部分会重叠。您可以根据自己的喜好安排位图。

      对于将图像重新保存为 png 的单独问题,请使用 bitmap.compress()。

      【讨论】:

        【解决方案6】:

        试试这个。

        public Bitmap mergeBitmap(Bitmap frame, Bitmap img){
        
            Bitmap bmOverlay = Bitmap.createBitmap(frame.getWidth(), frame.getHeight(), frame.getConfig());
            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(img, 0, 0, null);
            canvas.drawBitmap(frame, new Matrix(), null);
        
            return bmOverlay;
        
        }
        

        返回位图图像

        将两个位图图像传递给您的函数,如下所示

        Bitmap img= mergeBitmap(imgone, imagetwo);
        

        以编程方式查看entire post 或在android 中查看merge multiple images

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-27
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 2019-07-05
          • 1970-01-01
          • 2012-06-20
          相关资源
          最近更新 更多