【问题标题】:Android merge two imagesAndroid合并两张图片
【发布时间】:2021-12-15 20:44:59
【问题描述】:

我有这两个图像,我基本上在画布上合并。现在我想将该画布保存到图像中。我应该怎么做,或者是否有任何其他方法可以合并两个图像。

我的示例代码是 -

            Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.duckpic);
        Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.img);
        // canvas.drawColor(Color.BLACK);
        // canvas.drawBitmap(_scratch, 10, 10, null);
        Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2
                .getHeight(), bmp2.getConfig());
        // Canvas cs = new Canvas(bmp2);
        canvas.scale((float) 0.5, (float) 0.5);
        canvas.drawBitmap(bmp2, new Matrix(), null);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.save();

我这样做了 -

    cs = Bitmap.createBitmap(c.getWidth(), c.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(s, new Matrix(), null);
    comboImage.drawBitmap(c, new Matrix(), null);
    comboImage.save();
    // 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("/sdcard/" + tmpImg);
        cs.compress(CompressFormat.PNG, 100, os);
    } catch (IOException e) {
        Log.e("combineImages", "problem combining images", e);
    }

基本上在这里给出-http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

【问题讨论】:

标签: android


【解决方案1】:

使用canvas.setBitmap(Bitmap bitmap)。这会将画布发送到指定的位图。您需要为此创建一个新的、可变的位图。调用setBitmap 后,您可以将该位图保存到文件中。

【讨论】:

    【解决方案2】:
    Bitmap Final_image = null;
    Final_image = Bitmap.createBitmap(bitmap_first.getWidth(), bitmap_first.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(Final_image);
    comboImage.drawBitmap(bitmap_first, 0f, 0f, null);
    //this is X and Y for combineImages
    comboImage.drawBitmap(bitmap_second, 0f,bitmap_first.getHeight()-bitmap_second.getHeight()-50f, null);
    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".jpg";
    OutputStream os = null;
    try {
    os = new FileOutputStream("final" + tmpImg);
    Final_image.compress(Bitmap.CompressFormat.JPEG, 100, os);
    } catch(IOException e) {
    Log.e("combineImages", "problem combining images", e);
    }                            
    saveImage(Final_image);
    

    和保存功能:

    private void saveImage(Bitmap bitmap) throws IOException {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        Date date = new Date();
        CharSequence format = android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", date);
        String dirpath = Environment.getExternalStorageDirectory() + "";
        File file = new File(dirpath);
        if (!file.exists()) {
                boolean mkdir = file.mkdir();
                                }
        String path1 = dirpath + "/Documents/QSIM/SCREENSHOT/";
        File imageurl1 = new File(path1);
        imageurl1.mkdirs();
        File f = new File(path1 +"/"+ format + ".png");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2011-06-06
      • 2014-08-21
      相关资源
      最近更新 更多