【问题标题】:How to generate a PNG with Transparent Background from bitmap如何从位图生成具有透明背景的 PNG
【发布时间】:2022-01-31 10:56:10
【问题描述】:

我正在尝试生成一个带有签名的 png 文件。所以我希望它是透明背景,但我所能做的就是生成一个白色背景的签名,这不是我想要的输出。

这是我的代码。以防我遗漏了什么。

File file = new File(getExternalFilesDir(null), IndividualFragment.lastname.getText().toString().concat("_").concat(IndividualFragment.firstname.getText().toString()).concat("_sig") + ".png");
            FileOutputStream out = null;
            Bitmap bitmap = signatureView.getSignatureBitmap();
            try {
                out = new FileOutputStream(file);
                if (bitmap != null) {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                } else {
                    throw new FileNotFoundException();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.flush();
                        out.close();

                        if (bitmap != null) {
                            Toast.makeText(getApplicationContext(),
                                    "Image saved successfully at " + file.getPath(),
                                    Toast.LENGTH_LONG).show();
                            AttachmentsFragment.appPath.setText(file.getPath());
                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                                new MyMediaScanner(this, file);
                            } else {
                                ArrayList<String> toBeScanned = new ArrayList<String>();
                                toBeScanned.add(file.getAbsolutePath());
                                String[] toBeScannedStr = new String[toBeScanned.size()];
                                toBeScannedStr = toBeScanned.toArray(toBeScannedStr);
                                MediaScannerConnection.scanFile(this, toBeScannedStr, null,
                                        null);
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

在此先感谢各位,非常感谢您的帮助。

【问题讨论】:

标签: android bitmap


【解决方案1】:

供日后参考。

检查这个库。它对我有很大帮助。

https://github.com/gcacace/android-signaturepad

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 2012-12-19
    • 2011-06-23
    • 2013-12-10
    • 2014-03-21
    • 2016-10-24
    • 2021-12-02
    • 2014-07-07
    相关资源
    最近更新 更多