如何在图像上绘制圆圈

时间:2016-08-16 18:07:42

标签: android image-processing bitmapimage image-editing

我的应用程序从相机拍摄图像,保存并在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 个答案:

答案 0 :(得分:1)

如果您提供了有关您正在使用的库和语言的更多信息,那么它会很有帮助。从@override我会假设这是android上的java?

至于如何创建一个圆圈 - 您可以使用许多技术,并且可能有多个库可用于执行此操作。但是,我们可以通过在Bitmap对象的界面上使用函数来保持它非常简单,即getPixels和setPixels。

您需要做的是抓取一个像素矩形到预先分配的缓冲区(使用getPixels),然后将您的圆圈绘制到此缓冲区中,然后使用' setPixels'将缓冲区写回。   这是一个简单的(虽然不是非常有效)的方法,用于在缓冲区中绘制一个圆圈,你可以从getPixels'在javaish伪代码中(未经测试):

//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;
        }
}

这只是问一个问题,对于每个像素,&#39;是&#39; x,y&#39;在由'cx,cy,radius&#39;?&#39;给出的圆圈内如果是,则绘制一个像素。 更有效的方法可能包括扫描线光栅器,它可以穿过圆圈的左侧和右侧,无需进行昂贵的“距离”操作。计算每个像素。

然而,这个隐含的表面&#39;方法非常灵活,你可以用它实现很多效果。其他选项可能是复制预先制作的圆形位图而不是动态创建自己的位图。

你也可以混合颜色&#39;基于&#39;距离 - 半径&#39;的分数值。实现抗锯齿。

相关问题