2016-08-16 68 views
0

我的應用程序從相機拍攝圖像並保存,然後將其顯示在ImageView上,但下一步是在顯示的圖像頂部放置一個圓形,以便用戶觸摸屏幕,然後保存「修改的圖像」。如何在圖像頂部繪製圓形

有點像圖像編輯器,如果你願意,問題是我不知道從哪裏開始圖像編輯。我試過這個

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    circleView.setVisibility(View.VISIBLE); 
    circleView.setX(event.getX()-125); 
    circleView.setY(event.getY()-125); 

    try{ 
     Bitmap bitmap = Bitmap.createBitmap(relativeLayout.getWidth(),relativeLayout.getHeight(),Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     v.draw(canvas); 

     mImageView.setImageBitmap(bitmap); 
     FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()); 

     bitmap.compress(Bitmap.CompressFormat.PNG,100,output); 
     output.close(); 
    }catch(FileNotFoundException e){ 
     e.printStackTrace(); 
    }catch (IOException e){ 
     e.printStackTrace(); 
    } 


    return true; 
}//ENDOF onTouch 

我該怎麼做才能保存圖像?

回答

1

如果您包含更多關於您正在使用的庫和語言的信息,這將有所幫助。從@override我會認爲這是Android上的java?

至於如何創建一個圓 - 有很多技術可以使用,可能有多個庫可以用來做到這一點。但是,通過使用Bitmap對象的接口上的函數,即getPixels和setPixels,我們可以保持它非常簡單。

你需要做的是抓取一個像素矩形到預先分配的緩衝區(使用getPixels),然後畫出你的圓到這個緩衝區,然後用'setPixels'寫回緩衝區。 這裏有一個簡單的在你從javaish僞「的getPixels」獲得緩衝器繪製一個圓(雖然不完全有效)的方法(未經測試):

//Return the distance between the point 'x1, y1' and 'x2, y2' 
float distance(float x1, float y1, float x2, float y2) 
{ 
    float dx = x2 - x1; 
    float dy = y2 - y1; 
    return Math.sqrt(dx * dx + dy * dy); 
} 

//draw a circle in the buffer of pixels contained in 'int [] pixels' 
//at position 'cx, cy' with the given radius and colour. 
void drawCircle(int [] pixels, int stride, int height, float cx, float cy, float radius, int colour) 
{ 
    for (int y = 0; y < height; ++y) 
     for (int x = 0; x < stride; ++x) 
     { 
      if (distance((float)x, (float)y, cx, cy) < radius) 
       pixels[x + y * stride] = colour; 
     } 
} 

這只是問一個問題,對於每個像素, '是由'cx,cy,radius'給出的圓圈內的點'x,y'?'如果是,則繪製一個像素。 更高效的方法可能包括一個掃描線光柵器,它可以通過圓的左側和右側,不需要爲每個像素進行昂貴的「距離」計算。

但是,這種「隱式曲面」方法非常靈活,您可以通過它獲得很多效果。其他選項可能是複製預先製作的圓形位圖,而不是實時創建自己的位圖。

您還可以基於'距離 - 半徑'的分數值混合'顏色'以實現抗鋸齒。