7

キャンバスの内容をビットマップに配置することに関して、いくつかの問題があります。これを実行しようとすると、ファイルは約 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

同様の問題があり、解決策があります。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;
  }
于 2013-11-26T21:27:17.520 に答える
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 に答える
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 に答える
2

canvas.setBitmap(bitmap);Canvas に何かを描画する前に呼び出す必要があります。canvas.setBitmap(bitmap);draw onを呼び出した後、渡した をCanvas保存します。BitmapCanvas

于 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 に答える