Canvas is a Canvas. You can create a blank Canvas and create a new Canvas object without parameters. You can also use BitmapFactory to create a Bitmap object as a parameter for the new Canvas object. That is to say, the Canvas is not blank. If you want to save the image, it is better that Bitmap is a new one, instead of reading in from a file or a Drawable object. Use Canvas to draw the first image, draw the second image, and finally use Canvas. save (int flag) method. Note that the parameters in the save method can save a single layer. If you want to save all layers, use save (Canvas. ALL_SAVE_FLAG ). All the last information will be saved in the first created Bitmap. The Code is as follows: Java code
- /**
- * Create the bitmap from a byte array
- *
- * @ Param src the bitmap object you want proecss
- * @ Param watermark the water mark abve the src
- * @ Return a bitmap object, if paramter's length is 0, return null
- */
- Private Bitmap createBitmap (Bitmap src, Bitmap watermark)
- {
- String tag = "createBitmap ";
- Log. d (tag, "create a new bitmap ");
- If (src = null)
- {
- Return null;
- }
- Int w = src. getWidth ();
- Int h = src. getHeight ();
- Int ww = watermark. getWidth ();
- Int wh = watermark. getHeight ();
- // Create the new blank bitmap
- Bitmap newb = Bitmap. createBitmap (w, h, Config. ARGB_8888); // create a new Bitmap with the same length and width as SRC.
- Canvas cv = new Canvas (newb );
- // Draw src
- Cv. drawBitmap (src, 0, 0, null); // draw src at the coordinates of 0, 0.
- // Draw watermark
- Cv. drawBitmap (watermark, w-ww + 5, h-wh + 5, null); // Add a watermark to the bottom right corner of src.
- // Save all clip
- Cv. save (Canvas. ALL_SAVE_FLAG); // save
- // Store
- Cv. restore (); // Storage
- Return newb;
- }
Method for downgrading images: Java code
- /**
- * Lessen the bitmap
- *
- * @ Param src bitmap
- * @ Param destWidth the dest bitmap width
- * @ Param destHeigth
- * @ Return new bitmap if successful, oherwise null
- */
- Private Bitmap lessenBitmap (Bitmap src, int destWidth, int destHeigth)
- {
- String tag = "lessenBitmap ";
- If (src = null)
- {
- Return null;
- }
- Int w = src. getWidth (); // source file size
- Int h = src. getHeight ();
- // Calculate the scale-in this case = 0.4f
- Float scaleWidth = (float) destWidth)/w; // Width Reduction Ratio
- Float scaleHeight = (float) destHeigth)/h; // scale down the height
- Log. d (tag, "bitmap width is:" + w );
- Log. d (tag, "bitmap height is:" + h );
- Log. d (tag, "new width is:" + destWidth );
- Log. d (tag, "new height is:" + destHeigth );
- Log. d (tag, "scale width is:" + scaleWidth );
- Log. d (tag, "scale height is:" + scaleHeight );
- Matrix m = new Matrix (); // Matrix
- M. postScale (scaleWidth, scaleHeight); // sets the matrix ratio.
- Bitmap resizedBitmap = Bitmap. createBitmap (src, 0, 0, w, h, m, true); // directly draw the source file according to the matrix ratio.
- Return resizedBitmap;
- }