程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 拼圖代碼-兩張圖片拼接

拼圖代碼-兩張圖片拼接

編輯:C++入門知識

拼圖代碼-兩張圖片拼接


拼圖代碼——兩張圖片拼接:

onCreate函數:

 

    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);
    }
拼接函數:

 

方法一:

 

    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();
	//創建一個新的和SRC長度寬度一樣的位圖
	Bitmap newbmp=Bitmap.createBitmap(bgWidth+fgWidth, bgHeight+fgHeight, Config.ARGB_8888);
    	Canvas cv=new Canvas(newbmp);
    	cv.drawBitmap(background, 0, 0, null);//在0,0坐標開始畫bg
    	cv.drawBitmap(foreground, 0, bgHeight, null);//在0,0坐標開始畫fg,可以從任意位置畫入
    	
    	cv.save(Canvas.ALL_SAVE_FLAG);//保存
    	cv.restore();//存儲
    	
    	return newbmp;
    }

方法二:

 

 

    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();
	//創建一個新的和SRC長度寬度一樣的位圖
	Bitmap newbmp=Bitmap.createBitmap(bgWidth+fgWidth, bgHeight+fgHeight, Config.ARGB_8888);
    	Canvas cv=new Canvas(newbmp);
    	
    	//方法二
    	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;//X軸縮放比例
    	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);//保存
    	cv.restore();//存儲
    	
    	return newbmp;
    }
保存函數:

 

 

    //保存bitmap為一張圖片
    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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Toast.makeText(this, "保存失敗", 1).show();
			
			e.printStackTrace();
		}
    	
		return imagePath;
    	
    }


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved