How to bind any image with the captured Camera image before saving it? [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-03 21:42:32

Try this to combine the two Bitmaps the Camera Image and Transparent Image. This is combine of the images and store in the SDCard.

public Bitmap combineImages(Bitmap c, Bitmap s) {

        Bitmap cs = null;
        int width, height = 0;

        if (c.getWidth() > s.getWidth()) {
            width = c.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, 0, 0, null);
        comboImage.drawBitmap(s, 100, 300, null);

        /******
         * 
         *   Write file to SDCard
         * 
         * ****/

        String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
        OutputStream os = null;
        try {
            os = new FileOutputStream(Environment.getExternalStorageDirectory()
                    + "/"+tmpImg);
            cs.compress(CompressFormat.PNG, 100, os);
        } catch (IOException e) {
            Log.e("combineImages", "problem combining images", e);
        }
        return cs;
    }

Android implements several image compositing algorithms. Here's an easy way to use them. I don't have eclipse on this computer, so the following code is untested (but should work or at least be close to working).

img1 and img2 are both bitmaps, one of which you captured from the camera.

1) Create a new, empty bitmap. I'm assuming that img1 and img2 are the same size. If not, you can resize them or make this new bitmap have the size of the largest one or something.

Bitmap compositeImage = Bitmap.createBitmap(img1.getWidth(),
  img1.getWidth(), img1.getContig());

2) Create a canvas for drawing on the bitmap

Canvas canvas = new Canvas(compositeImage);

3) draw the first image onto the new bitmap

Paint paint = new Paint();
canvas.drawBitmap(img1, 0.0f, 0.0f, paint);

4) Android has a set of compositing algorithms in the SDK. The methods are named after Porter and Duff, who wrote a paper describing algorithms to composite images. Android calls this setting "transfermode". To see the available Porter-Duff transfermodes, go to http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html or google it. For your needs, I would recommend looking at Multiply, Darken, and Lighten.

paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));

5) Now that the transfermode is set, simply draw the second image to obtain the effect you want

canvas.drawBitmap(img2, 0.0f, 0.0f, paint);

Now, the compositeImage Bitmap contains the combined image.

Edit. Ok, here is the code put together, maybe you could put it in your PictureCallback (the jpeg one, not the raw one). Again, it's untested but should be good or close to good:

public void onPictureTaken(byte[] data, Camera camera) {
  Bitmap img1 = BitmapFactory.decodeResource(getResources(), 
    R.drawable.icon); // assuming you really want to use the icon
  Bitmap img2 = BitmapFactory.decodeByteArray(data, 0, data.length);
  Bitmap composite = Bitmap.createBitmap(img2.getWidth(), img2.getHeight(), 
    img2.getConfig());
  Canvas canvas = new Canvas(composite);
  Paint paint = new Paint();
  canvas.drawBitmap(img2, 0.0f, 0.0f, paint);
  paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));
  canvas.drawBitmap(img1, 0.0f, 0.0f, paint);

  // It seems you want to insert the new image into the gallery
  ContentValues values = ...
  try {
    Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
    composite.compress(Bitmap.CompressFormat.JPEG, 90, 
      getContentResolver().openOutputStream(imageFileUri));
  } catch(Exception) {
    ...
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!