画布位图到字节数组并读回麻烦

发布于 2025-01-07 22:53:52 字数 2011 浏览 3 评论 0原文

我有一个可以绘制的画布,我试图取出位图,将其转换为字节数组并将其序列化保存到文件中。然后稍后打开、反序列化并将位图应用回画布。在下面的代码中,一切似乎都运行良好,除了将位图应用到画布时什么也没有出现。有人可以告诉我哪里出了问题吗?

public byte[] getCanvasData(){

    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    mBitmap.compress(CompressFormat.PNG, 0, bos); 
    byte[] bitmapdata = bos.toByteArray();
    return bitmapdata;
}

public void setCanvasData(byte[] canvasData, int w, int h){
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mBitmap.eraseColor(0x00000000);
    mCanvas = new Canvas(mBitmap);
    mCanvas.drawBitmap(BitmapFactory.decodeByteArray(canvasData , 0,  canvasData.length).copy(Bitmap.Config.ARGB_8888, true), 0, 0, null);

添加

了一些额外的代码以可能帮助一点

public void readInSerialisable() throws IOException
{
    FileInputStream fileIn = new FileInputStream("/sdcard/theBKup.ser");

    ObjectInputStream in = new ObjectInputStream(fileIn);


    try
    { 
       BookData book = (BookData) in.readObject();
       pages.clear();
       canvasContainer.removeAllViews();

       for (int i = 0; i < book.getBook().size(); i++){
           Log.d("CREATION", "LOADING PAGE " + i);
           pages.add(new Canvas2(context, book.getPageAt(i), canvasContainer.getWidth(), canvasContainer.getHeight()));
       }

       canvasContainer.addView(pages.get(page), new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams.FILL_PARENT, 0, 0));

       updatePagination();
       Log.d("CREATION", "Updated Pagination");
    }
    catch (Exception exc)
    {
        System.out.println("didnt work");
        exc.printStackTrace();
    }
}

BookData - 包含我所有数据的可序列化类,其中

onDraw 方法中的简单获取/设置

 @Override
protected void onDraw(Canvas canvas) {
    Log.d("DRAWING", "WE ARE DRAWING");
    canvas.drawColor(0x00AAAAAA); //MAKE CANVAS TRANSPARENT
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
}

I have a Canvas I draw on, I'm trying to take the bitmap out, convert it to a byte array and save it serialized into a file. then later open, deserialize, and apply the bitmap back to the canvas. In the code below everything seems to work well except that when applying the bitmap to canvas nothing appears. can someone please show me where I'm going wrong.

public byte[] getCanvasData(){

    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    mBitmap.compress(CompressFormat.PNG, 0, bos); 
    byte[] bitmapdata = bos.toByteArray();
    return bitmapdata;
}

public void setCanvasData(byte[] canvasData, int w, int h){
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mBitmap.eraseColor(0x00000000);
    mCanvas = new Canvas(mBitmap);
    mCanvas.drawBitmap(BitmapFactory.decodeByteArray(canvasData , 0,  canvasData.length).copy(Bitmap.Config.ARGB_8888, true), 0, 0, null);

}

ADDED SOME EXTRA CODE TO POSSIBLY HELP A LITTLE

public void readInSerialisable() throws IOException
{
    FileInputStream fileIn = new FileInputStream("/sdcard/theBKup.ser");

    ObjectInputStream in = new ObjectInputStream(fileIn);


    try
    { 
       BookData book = (BookData) in.readObject();
       pages.clear();
       canvasContainer.removeAllViews();

       for (int i = 0; i < book.getBook().size(); i++){
           Log.d("CREATION", "LOADING PAGE " + i);
           pages.add(new Canvas2(context, book.getPageAt(i), canvasContainer.getWidth(), canvasContainer.getHeight()));
       }

       canvasContainer.addView(pages.get(page), new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams.FILL_PARENT, 0, 0));

       updatePagination();
       Log.d("CREATION", "Updated Pagination");
    }
    catch (Exception exc)
    {
        System.out.println("didnt work");
        exc.printStackTrace();
    }
}

BookData - Serializable class containing all my data, simple gets/sets in there

onDraw Method

 @Override
protected void onDraw(Canvas canvas) {
    Log.d("DRAWING", "WE ARE DRAWING");
    canvas.drawColor(0x00AAAAAA); //MAKE CANVAS TRANSPARENT
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2025-01-14 22:53:52

我会做以下两个测试。

  1. 记录一些字节流以确保其加载正确。像 Log.v(canvasData[0]+canvasData[1]); 这样的东西,或者在那里放置一个断点,或者只是为了确保数据正确。
  2. 使用相同的代码绘制一个您知道有效的位图,并查看它是否正确显示。

我不确定到底发生了什么,但我强烈怀疑以下情况之一。

  1. 字节流未正确读取。
  2. 位图未更新到屏幕,或者使用的尺寸非常小。

如果您的字节流数据有某些内容,那么您将需要查看 Canvas 文档。 具体来说,请查看以下内容。

为了看到画布,必须将其放在视图上。一旦它位于视图上,就必须调用 onDraw() 命令才能使其可见。我会确保您实际上正在执行 onDraw(),并且 Canvas 与 View 正确关联。如果您已经使用 onDraw(),请发布与其相关的代码位。

I would do the following 2 tests.

  1. Log some of the byte stream to make sure that it was loaded correctly. Something like Log.v(canvasData[0]+canvasData[1]);, or put a break point there, or something just to make sure the data is correct.
  2. Draw a bitmap that you know is valid, using the same code, and see if it appears correctly.

I'm not sure exactly what's going on, but I strongly suspect one of the following.

  1. The byte stream is not being read in correctly.
  2. The bitmap is not being updated to the screen, or is using a trivially small size.

In the event that your byte stream data has something, then you will want to take a look at the Canvas documentation. Specifically, look at the following bit.

In order to see a Canvas, it has to be put on to a view. Once it is on a view, the onDraw() command must be called for it to be visible. I would make sure that you are in fact doing an onDraw(), and that the Canvas is associated with the View correctly. If you are using an onDraw() already, please post the bits of code associated with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文