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

我该怎么做才能保存图像?

4

1 回答 1

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

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

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

于 2016-08-16T18:32:27.903 回答