如何在Android中获取PNG格式的图像

时间:2019-07-02 06:31:54

标签: android bitmap itext png android-glide

我有一个图像正在在线服务器中保存。

这就是为什么我必须首先将图像转换为字符串的原因。 但我也想以PDF格式显示这张图片。因此,如何将这张图片用作PNG。

或者还有其他方法可以使我使用iTextpdf在PDF上显示它。我很困惑。我搜索了很多,但没有任何帮助。

这是我的代码(MainActivity):

String imageName = null;
final ImageSaver imageSaver = new ImageSaver(MainActivity.this);
if (imageUri != null && !imageUri.isEmpty()) {
    imageName = imageSaver.getFileName();
    Glide.with(MainActivity.this).asBitmap().load(imageUri)
        .listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                // TODO : upload image here...
                imageSaver.save(resource);
                return true;
            }
        }).submit();
} else {
    Log.e(TAG, "uploadImageToServer: no image to upload!!!");
}

ImageSaver类:

public class ImageSaver {

    private static String directoryName = "images";

    public String getFileName() {
        return fileName;
    }

    private String fileName;
    private Context context;
    public ImageSaver(Context context) {
        this.context = context;
        this.fileName = generateFileName();
    }

    public String save(Bitmap bitmapImage) {
        String result= "";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(createFile());
            bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            result = fileName;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return  result;
    }

    @NonNull
    private File createFile() {
        File directory = context.getDir(directoryName, Context.MODE_PRIVATE);
        if(!directory.exists() && !directory.mkdirs()){
            Log.e("ImageSaver","Error creating directory " + directory);
        }

        return new File(directory, fileName);
    }

    public static byte[] getImage(Context context, String imageName)
    {
        try {
            File directory = context.getDir(directoryName, Context.MODE_PRIVATE);
            File image = new File(directory, imageName);
            Bitmap b = BitmapFactory.decodeStream(new FileInputStream(image));
            //return BitmapUtils.convertBitmapToByteArrayUncompressed(b);
            return BitmapUtils.convertBitmapToByteArray(b);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }

    }

    private static String generateFileName() {
        DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        Date date = new Date();
        return "image_" + dateFormat.format(date) + ".jpeg";
    }
}

我以前从像这样的资产文件夹中添加了图片:

try {
            InputStream ims = getAssets().open("naam.PNG");
            Bitmap bmp = BitmapFactory.decodeStream(ims);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            Image image = Image.getInstance(stream.toByteArray());
            image.scaleToFit(75, 90);
            image.setAlignment(Element.ALIGN_CENTER);

            cl = new PdfPCell(image, true);
            cl.setHorizontalAlignment(Element.ALIGN_CENTER);
            cl.setVerticalAlignment(Element.ALIGN_CENTER);
            cl.setBorderColor(BaseColor.WHITE);
            cl.setFixedHeight(21);
            tableclerk.addCell(cl);
        }
        catch(IOException ex) { return; }
        tableclerk.setWidths(columnWidthsc);
        mDoc.add(tableclerk);

1 个答案:

答案 0 :(得分:0)

只需将您的图片保存为.PNG格式

 bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);