开发者

Android Bitmap problem

开发者 https://www.devze.com 2023-03-28 07:06 出处:网络
I have a motion JPEG stream that is put on a canvas in Android.My problem i开发者_如何学Gos that at the bottom right of the canvas there is a little, gray, pixelated bar.It kinda of disappears and re-

I have a motion JPEG stream that is put on a canvas in Android. My problem i开发者_如何学Gos that at the bottom right of the canvas there is a little, gray, pixelated bar. It kinda of disappears and re-appears. I don't have this issue on other platforms that use the same stream, so i'm guessing its an Android problem. Here is my code:

Canvas canvas = null;
            try
            {

                Bitmap bmp = BitmapFactory.decodeByteArray(notifi.imgData, 0, notifi.imgData.length);
                if (bmp == null)
                    System.out.println("Skipping invalid MJpeg frame");
                else
                {
                    canvas = holder.lockCanvas(null);
                    if (canvas == null)
                    {
                        System.out.println("Cannot lock canvas, skipping MJpeg frame");
                        return;
                    }
                    canvas.drawColor(Color.BLACK);

                    Rect dst = null;
                    int viewWidth = mPreview.getWidth();
                    int viewHeight = mPreview.getHeight();
                    float ratio = bmp.getWidth() / (float)bmp.getHeight();
                    int desiredHeight = (int)(viewWidth / ratio);
                    if (desiredHeight > viewHeight)
                    {   // Letterbox
                        int maxWidth = (int)(viewHeight * ratio);
                        int pad = (viewWidth - maxWidth) / 2;
                        dst = new Rect(pad, 0, maxWidth + pad, viewHeight);
                    }
                    else
                    {
                        int pad = (viewHeight - desiredHeight) / 2;
                        dst = new Rect(0, pad, viewWidth, desiredHeight + pad);
                    }

                    canvas.drawBitmap(bmp, null, dst, null);
                }
            } finally {
                if (canvas != null)
                    holder.unlockCanvasAndPost(canvas);
            }
        }
        });


canvas = holder.lockCanvas(null); looks suspect.

Try canvas = holder.lockCanvas(); instead.


I notice that you are adding a "Pad" to the destination rectangle to change it's height.

If you do that, I wonder if the destination rectangle size will match the size of the image you are drawing and thus "over draw" some gray bar as you are seeing. Just a guess - maybe if you remove the height pad it will go away? Just a guess.

dst = new Rect(0, pad, viewWidth, desiredHeight + pad);
canvas.drawBitmap(bmp, null, dst, null);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号