【问题标题】:Saving canvas to bitmap on Android在 Android 上将画布保存到位图
【发布时间】:2013-02-06 17:30:28
【问题描述】:

我在将 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();

        }
    }

【问题讨论】:

    标签: java android android-canvas bitmapfactory


    【解决方案1】:

    我有类似的问题,我有解决方案。这里是任务的完整代码/不要忘记清单中的android.permission.WRITE_EXTERNAL_STORAGE 权限/

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

    【讨论】:

    • 获得空白 png
    • 我获取空白图像。这个问题有什么解决办法吗??
    【解决方案2】:

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

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

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

    【讨论】:

    • 感谢反馈,如果这个操作要从 onDraw() 方法中取出,使用 Thread 或 AsyncTask 什么的,onDraw(Canvas) 画布对象如何传递给他们?这样我就可以应用 canvas.setBitmap() 例程了吗?谢谢。
    • @ArronJeffery 编辑了我的答案。
    • @DheerajV.S.我有同样的事情:用位图创建了一个画布并绘制了一条路径。它可以正确显示(使用 ImageView.setImageBitmap),但 Bitmap.compress 返回 null 并创建了无效文件。
    • 你找到解决办法了吗@vedant1811
    • 你找到解决方案了吗@user1185687
    【解决方案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

    访问您的位图

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 您好,感谢您的反馈。我试过这个没有成功,因为我在应用这个时不断收到 UnsupportedOperationException 。截图:i.imgur.com/eHHzY5Z.png。这必须在 onDraw 方法中完成,对吧?至于你怎么能得到“画布”句柄?
      【解决方案5】:

      可能

      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);
      

      【讨论】:

      • 不,不幸的是它没有工作。不过感谢您的提示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多