【问题标题】:How to draw a circle on top of an image如何在图像上画一个圆圈
【发布时间】:2016-08-16 18:07:41
【问题描述】:

我的应用程序从相机中获取图像,将其保存并显示在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

如何保存图片?

【问题讨论】:

    标签: android image-processing bitmapimage image-editing


    【解决方案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' 是吗?如果是,它会绘制一个像素。 更有效的方法可能包括一个扫描线光栅化器,它逐步穿过圆的左右两侧,从而无需为每个像素进行昂贵的“距离”计算。

    但是,这种“隐式表面”方法非常灵活,您可以使用它实现很多效果。其他选项可能是复制预制的圆形位图,而不是动态创建自己的。

    您还可以根据“距离-半径”的小数值混合“颜色”以实现抗锯齿。

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2010-12-24
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多