Jigsaw Puzzle code-splice two images
Jigsaw Puzzle code -- splice two images:
OnCreate function:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview=(ImageView) findViewById(R.id.imageview); Bitmap background=BitmapFactory.decodeResource(getResources(), R.drawable.back); Bitmap foreground=BitmapFactory.decodeResource(getResources(), R.drawable.plane); /*Canvas canvas=new Canvas(background); drawImage(canvas, background, 0, foreground.getHeight(), foreground.getWidth(), background.getHeight()/2, 0, 0);*/ Bitmap bitmap=toConformBitmap(background, foreground); imageview.setImageBitmap(bitmap); }
Splicing function:
Method 1:
Private Bitmap toConformBitmap (Bitmap background, Bitmap foreground) {if (background = null) {return null;} int bgWidth = background. getWidth (); int bgHeight = background. getHeight (); int fgWidth = foreground. getWidth (); int fgHeight = foreground. getHeight (); // create a new Bitmap with the same length and width as SRC. Bitmap newbmp = Bitmap. createBitmap (bgWidth + fgWidth, bgHeight + fgHeight, Config. ARGB_8888); Canvas cv = new Canvas (newbmp); cv. drawBitmap (background, 0, 0, null); // start to draw bg cv at coordinates 0, 0. drawBitmap (foreground, 0, bgHeight, null); // draw fg at the coordinate of 0 and 0, and draw cv from any position. save (Canvas. ALL_SAVE_FLAG); // Save the cv. restore (); // store return newbmp ;}
Method 2:
Private Bitmap toConformBitmap (Bitmap background, Bitmap foreground) {if (background = null) {return null;} int bgWidth = background. getWidth (); int bgHeight = background. getHeight (); int fgWidth = foreground. getWidth (); int fgHeight = foreground. getHeight (); // create a new Bitmap with the same length and width as SRC. Bitmap newbmp = Bitmap. createBitmap (bgWidth + fgWidth, bgHeight + fgHeight, Config. ARGB_8888); Canvas cv = new Canvas (newbmp); // method 2 Rect dst = new Rect (); dst. left = 0; dst. top = 0; dst. right = bgWidth; dst. bottom = bgHeight-50; cv. drawBitmap (background, null, dst, null); Rect dst2 = new Rect (); dst2.left = 0; dst2.top = bgHeight; double Xscale = bgWidth/fgWidth; // zooming ratio of X axis dst2.bottom = (int) (bgHeight + fgHeight * Xscale); dst2.right = bgWidth; cv. drawBitmap (foreground, null, dst2, null); dst2 = null; dst = null; cv. save (Canvas. ALL_SAVE_FLAG); // Save the cv. restore (); // store return newbmp ;}
Save function:
// Save bitmap as an image private String saveBitmap (Bitmap bitmap) {String imagePath = getApplication (). getFilesDir (). getAbsolutePath () + "/temp.jpg"; File file = new File (imagePath); if (file. exists () {file. delete ();} try {FileOutputStream out = new FileOutputStream (file); if (bitmap. compress (Bitmap. compressFormat.. PNG, 100, out) {out. flush (); out. close () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blockToast. makeText (this, "failed to save", 1 ). show (); e. printStackTrace ();} return imagePath ;}