复制位图问题

发布于 2024-12-01 05:40:39 字数 1062 浏览 3 评论 0原文

我有一个问题要问。我只想在以下代码中将文本添加到位图中。当我调试到 Bitmap bmp = bmp1.copy(bmp1.getConfig(), true); 监视窗口中的一切正常时,我可以看到宽度和高度为 640 和 480。

但是在执行复制方法之后它返回位图为bmp,但bmp的宽度和高度为-1和-1,并且无法添加文本。有人可以告诉我为什么以及如何解决这个问题吗?多谢

    String strPath=Environment.getExternalStorageDirectory().toString();
    String strFileNameIn="aaa.jpg";
    File inFile=new File(strPath+File.separator+strFileNameIn); 
    try {
        if (inFile.exists()) {
    FileInputStream inStream = new FileInputStream(inFile);
    BitmapDrawable bmpDraw = new BitmapDrawable(inStream);
        Bitmap bmp1 = bmpDraw.getBitmap();
    Bitmap bmp = bmp1.copy(bmp1.getConfig(), true);

    Canvas cv = new Canvas(bmp);
    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strText = sDateFormat.format(new Date());
        Paint p = new Paint();
    p.setColor(Color.RED);

    cv.drawText(strText, 0, 0, p);
    cv.save(Canvas.ALL_SAVE_FLAG);
    cv.restore();
    }
        } catch (Exception e) {
          String strMsg = e.getMessage();
        }

I've got a question to ask. I just want to add a text to a bitmap in following code. when I debug to Bitmap bmp = bmp1.copy(bmp1.getConfig(), true); everything worked fine in watch window I can see width and height with 640 and 480.

but after excute the copy method it returns the Bitmap to bmp, but the width and height of bmp is -1 and -1, and it couldn't add the text. could some guys told me why and how to fix the problem. thanks a lot

    String strPath=Environment.getExternalStorageDirectory().toString();
    String strFileNameIn="aaa.jpg";
    File inFile=new File(strPath+File.separator+strFileNameIn); 
    try {
        if (inFile.exists()) {
    FileInputStream inStream = new FileInputStream(inFile);
    BitmapDrawable bmpDraw = new BitmapDrawable(inStream);
        Bitmap bmp1 = bmpDraw.getBitmap();
    Bitmap bmp = bmp1.copy(bmp1.getConfig(), true);

    Canvas cv = new Canvas(bmp);
    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strText = sDateFormat.format(new Date());
        Paint p = new Paint();
    p.setColor(Color.RED);

    cv.drawText(strText, 0, 0, p);
    cv.save(Canvas.ALL_SAVE_FLAG);
    cv.restore();
    }
        } catch (Exception e) {
          String strMsg = e.getMessage();
        }

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

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

发布评论

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

评论(1

您可以使用 bitmapfactory 更轻松地获取位图。

这个怎么样:

Bitmap bitmap = BitmapFactory.decodeFile("your file");
if (bitmap != null) {
    Paint p = new Paint();
    Canvas cv = new Canvas();
    cv.drawBitmap(bitmap, 0, 0, p);
    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strText = sDateFormat.format(new Date());
    p.setColor(Color.RED);

    cv.drawText(strText, 0, 0, p);
    cv.save(Canvas.ALL_SAVE_FLAG);
    cv.restore();
}

You can get a bitmap more easily with bitmapfactory.

How about this:

Bitmap bitmap = BitmapFactory.decodeFile("your file");
if (bitmap != null) {
    Paint p = new Paint();
    Canvas cv = new Canvas();
    cv.drawBitmap(bitmap, 0, 0, p);
    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strText = sDateFormat.format(new Date());
    p.setColor(Color.RED);

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