7

我在将 Canvas 的内容放入位图中时遇到了一些困难。当我尝试执行此操作时,该文件以大约 5.80KB 的文件大小写入,但它似乎完全是空的(每个像素都是“#000”)。

画布绘制了一系列由手写形成的相互连接的线条。下面是我的 onDraw 视图。(我知道它阻塞了 UI 线程/不良做法/等等,但是我只需要让它工作)

谢谢你。

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

        if (IsTouchDown) {

            // Calculate the points
            Path currentPath = new Path();
            boolean IsFirst = true;
            for(Point point : currentPoints){
                if(IsFirst){
                    IsFirst = false;
                        currentPath.moveTo(point.x, point.y);
                    } else {
                        currentPath.lineTo(point.x, point.y);
                    }
                }

            // Draw the path of points
            canvas.drawPath(currentPath, pen);

            // Attempt to make the bitmap and write it to a file.
            Bitmap toDisk = null;
            try {

                // TODO: Get the size of the canvas, replace the 640, 480
                toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
                canvas.setBitmap(toDisk);
                toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));

            } catch (Exception ex) {


            }

        } else {

            // Clear the points
            currentPoints.clear();

        }
    }
4

5 回答 5

13

I had similar problem and i've got solution. Here full code of a task /don't forget about android.permission.WRITE_EXTERNAL_STORAGE permission in manifest/

  public Bitmap saveSignature(){

      Bitmap  bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      this.draw(canvas); 

      File file = new File(Environment.getExternalStorageDirectory() + "/sign.png");

      try {
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
      } catch (Exception e) {
           e.printStackTrace();
      }

      return bitmap;
  }
于 2013-11-26T21:27:17.520 回答
3

首先创建一个空白位图,然后使用该空白位图创建一个画布

 Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
 Bitmap bitmap_object = Bitmap.createBitmap(width, height, conf); 
 Canvas canvas = new Canvas(bitmap_object);

现在在画布上画线

       Path currentPath = new Path();
        boolean IsFirst = true;
        for(Point point : currentPoints){
            if(IsFirst){
                IsFirst = false;
                    currentPath.moveTo(point.x, point.y);
                } else {
                    currentPath.lineTo(point.x, point.y);
                }
            }

        // Draw the path of points
        canvas.drawPath(currentPath, pen);

现在通过bitmap_object访问您的位图

于 2013-02-21T21:37:56.393 回答
3

将位图设置为画布,您必须进行绘制。还可以像这样使用一个新的 Canvas 对象:

Canvas canvas = new Canvas(toDisk);
canvas.drawPath(currentPath, pen);
toDisk.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File("arun.png")));

我建议使用 PNG 来保存路径图像。

于 2013-02-21T12:01:52.867 回答
2

canvas.setBitmap(bitmap);在 Canvas 上绘制任何内容之前,您必须先致电。在调用canvas.setBitmap(bitmap);draw onCanvas然后保存Bitmap你传递给Canvas.

于 2013-02-21T12:03:10.817 回答
0

或许

canvas.setBitmap(toDisk);

位置不正确。

尝试这个 :

toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);              
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));

canvas.setBitmap(toDisk);
于 2013-02-21T11:56:45.980 回答