【问题标题】:Telegram Bot Java Library Downloaded Image QualityTelegram Bot Java 库下载的图像质量
【发布时间】:2017-01-30 16:19:32
【问题描述】:

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

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

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;
}

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

  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();

【问题讨论】:

  • 如果你来自 rubenlagus,为什么不和 rubenlagus 谈谈??
  • 这与 JPA 有什么关系?你有一些图像字节,你坚持它并检索它......它会是一样的。所以问题不在于JPA。所以调试问题出在哪里
  • @gpasch 已经完成了,还没有运气..

标签: java jpa bots telegram telegram-bot


【解决方案1】:

这里有几种可能性:

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

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

希望这能解释问题。

【讨论】:

  • 谢谢鲁本。它奏效了。
猜你喜欢
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 2019-11-18
  • 1970-01-01
相关资源
最近更新 更多