开发者

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

开发者 https://www.devze.com 2023-04-13 02:45 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Android: How to overlay-a-bitmap/draw-over a bitmap?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Android: How to overlay-a-bitmap/draw-over a bitmap?

I have implement the android Camera functionality in amy application.

Now i want to combine any transperent image with that captured camera Image before to save that image in to gallery, then how it can be done ??

Any code will be realy appereciated. Thanks.

Edit: I am using this Code to captured and Sav开发者_开发问答e the Image So, How shoud i have to Implement Such things.

takePhotoBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) { // <5>

            ImageCaptureCallback iccb = null;

            try {
                String filename = timeStampFormat.format(new Date());
                ContentValues values = new ContentValues();
                values.put(Media.TITLE, filename);
                values.put(Media.DESCRIPTION, "Image capture by camera");
                Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

                iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));

                // to put the Image on captured image.
                Canvas canvas = new Canvas();
                Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                        R.drawable.icon);
                canvas.drawColor(Color.argb(160, 21, 140, 21));
                canvas.drawBitmap(kangoo, 130, 10, null);


            } catch(Exception ex ){
                ex.printStackTrace();
                Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
            }
          camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb);
        }
      });


Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera c) {
        Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
            System.out.println("\n\n\n\nThe Data in mPictureCallbackRaw is :"+data);
        //camera.startPreview(); // Added StastPreview();
    }
};

Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera c) {
        Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
        System.out.println("\n\n\n\nThe Data in mPictureCallbackJPEG is :"+data);
        camera.startPreview();
    }
};

Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
        Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
    }
};




public class ImageCaptureCallback implements PictureCallback  {

private OutputStream filoutputStream;
public ImageCaptureCallback(OutputStream filoutputStream) {
    this.filoutputStream = filoutputStream;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    try {
        Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " length = " + data.length);

        filoutputStream.write(data);
        filoutputStream.flush();
        filoutputStream.close();


    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

}


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) {
    ...
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号