Telegram Bot Java 库下载的图像质量 [英] Telegram Bot Java Library Downloaded Image Quality

查看:50
本文介绍了Telegram Bot Java 库下载的图像质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rubenlagus 的 TelegramBots Java API 来开发我的 Telegram Bot.我能够成功地向机器人发送照片并从 Telegram 中检索照片,如 这个例子.问题是与上传的实际图像相比,下载的图像较小且质量较差.我使用 Java Persistence (JPA) 将图像存储为 blob.这是我的代码

I am using TelegramBots Java API by rubenlagus to develop my Telegram Bot. I am able to successfully send a photo to the bot and also retrieve the photo from Telegram as illustrated by this example . Problem is that the image downloaded is smaller and also poor in quality as compared to actual image uploaded. Am storing the image as a blob using Java Persistence (JPA). Here's my code

从客户端接收图像的示例代码;

Sample Code That Receives Image from Client;

List<PhotoSize> photos = message.getPhoto();
            System.out.println("Photos --> " + photos.size());
            for (int i = 0; i < photos.size(); i++) {

                GetFile getFileRequest = new GetFile();

                getFileRequest.setFileId(photos.get(i).getFileId());
                File file = getFile(getFileRequest);
                //  System.out.println(file.getFilePath());
                downloadFilePath = filePathUrl + file.getFilePath();
                System.out.println("Photo --> " + downloadFilePath);
                java.io.File fileFromSystem =downloadFile(downloadFilePath);

                byte[] bytes = new byte[(int) fileFromSystem.length()];

                System.out.println( photo Size --> " + bytes.length);

                FileInputStream fileInputStream = new FileInputStream(fileFromSystem);
                fileInputStream.read(bytes);
                myEntity.setPhoto(bytes);
                myFacade.edit(myEntity);

下载文件方法;

private java.io.File downloadFile(String fileUrl) {
    java.io.File file = null;
    try {

        sysProps = System.getProperties();
        URL url = new URL(fileUrl);
        InputStream in = url.openStream();
        String directoryPath = sysProps.getProperty("file.separator") + sysProps.getProperty("user.home") + sysProps.getProperty("file.separator") + "Documents" + sysProps.getProperty("file.separator") + "dev";
        java.io.File directory = new java.io.File(directoryPath);

        String pathToFile = directoryPath + sysProps.getProperty("file.separator") + new Random().nextInt(100) + fileUrl.substring(fileUrl.lastIndexOf("/") + 1);

        if (!directory.exists()) {
            directory.mkdirs();
        }
        file = new java.io.File(pathToFile);
        file.createNewFile();

        FileOutputStream outputStream = new FileOutputStream(file);
        int read = 0;

        byte[] bytes =  new byte[10000];
        while ((read = in.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);

        }
        outputStream.flush();
        outputStream.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return file;
}

向客户端发送图像的代码(从实体转换字节[]并发送到客户端)

Code That Sends Image To Client(Converts bytes[] from entity and sends to client)

  String strFilePath = sysProps.getProperty("user.home") + sysProps.getProperty("file.separator") + "Documents" + sysProps.getProperty("file.separator") + "dev" + sysProps.getProperty("file.separator") + new Random().nextInt(100) + ".jpeg";
                FileOutputStream fos = new FileOutputStream(strFilePath);
                fos.write(myEntity.getPhoto());
                fos.close();

                SendPhoto sendPhotoRequest = new SendPhoto();
                sendPhotoRequest.setChatId(message.getChatId().toString());
                java.io.File fileToSend = new java.io.File(strFilePath);
                sendPhotoRequest.setNewPhoto(fileToSend);

                //    System.out.println("Sending phtoto -->   " + strFilePath );
                sendPhoto(sendPhotoRequest);
                fileToSend.delete();

推荐答案

这里有几种可能性:

  1. 当您在 bot api 中收到 Message 时,photo 对象是 PhotoSize 的数组.您需要确保下载的是该数组中较大的(检查widthheigth 参数).

  1. When you get a Message in the bot api, the photo object is an array of PhotoSize. You need to make sure you are downloading the bigger one in that array (checking width and heigth parameters).

当使用 sendPhoto 方法发送照片时,电报压缩照片(与从任何官方应用发送照片相同.这意味着您的照片可能会损失一些质量.如果您不希望发生这种情况,您可以随时使用 sendDocument 方法.

When sending a photo using sendPhoto method, Telegram compress the photo (same as sending the photo from any of the official apps. This mean your photo may lose some quality. If you don't want this to happen, you can always use sendDocument method.

希望这能解释问题.

这篇关于Telegram Bot Java 库下载的图像质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆