Android camera development: Add a photo frame after taking a photo, and combine the photo frame and image into a pair of images

Source: Internet
Author: User

To add a photo frame, you can create a bitmap and instantiate a canvas accordingly. Then draw the source image and the frame in sequence.

In the onpicturetaken () function, obtain the original bitmap, get the photo frame, and then call the fusion function.

Bitmap frame = bitmapfactory. decoderesource (getresources (), R. drawable. Border );
Bitmap monbm = montagebitmap (frame, sizebitmap, 200,200 );


 

/* Combine the image frame and image, and return a bitmap */Public bitmap montagebitmap (Bitmap frame, bitmap SRC, int X, int y) {int W = SRC. getwidth (); int H = SRC. getheight (); bitmap sizeframe = bitmap. createscaledbitmap (frame, W, H, true); bitmap newbm = bitmap. createbitmap (W, H, config. argb_8888); canvas = new canvas (newbm); canvas. drawbitmap (SRC, X, Y, null); canvas. drawbitmap (sizeframe, 0, 0, null); Return newbm ;}

In the program, frame represents the photo frame. SRC represents the original image, and the size is 600*800. First, scale the size of the image frame to 600*800, and then instantiate a canvas. Remember to draw the source image first. There is an X and Y coordinate.
Here is the annotation of this API:

public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)Added in API level 1Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas.ParametersbitmapThe bitmap to be drawnleftThe position of the left side of the bitmap being drawntopThe position of the top side of the bitmap being drawnpaintThe paint used to draw the bitmap (may be null)

Looking at the above explanation, it seems that it is unclear who is the x y coordinate, whether it is the source image or the canvas coordinate? What if the size of the image to be drawn exceeds the size of the canvas? According to the actual test, here, the X and Y coordinates refer to the vertices (x, y) on the canvas to draw bitmap. If the bitmap size exceeds the canvas size, it is not displayed. The following two groups of test images can be seen clearly.

 

The first group of test photos (x, y) = (20, 20 ):

Source image:

 

Source image + photo frame:

 

The second group (x, y) = (200,200 ):

Source image:

 

Source image + photo frame:

As you can see, when the transmitted coordinates are smaller than hours, it cannot be seen any difference. In fact, if we set both coordinates to (0, 0), we will see the stacked effect of two photos of the same size. This is how to define the photo frame. If you do not want to block the source image, you need to scale the source image to the rect size. This rect refers to the size of the blank (transparent) Part in the photo frame. Then draw the scaled source image from the top left vertex of the transparent part of the canvas.

Success! For more information, see.

--------------------------------------------------------------------- This article is original. For details, see Author: yanzi1225627.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.