有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jpeg如何在使用java代码上传到Bugzilla工具时获得图像的实际颜色而不失真?

**我正在使用下面的代码将多个故障截图从文件夹中提取到Bugzilla工具,在Bugzilla中上载图片时,图片的颜色被破坏。[在此处输入图像描述][1]。有人能帮我纠正这个问题吗?**

             try {
                 BugzillaConnector conn = new BugzillaConnector();
                 conn.connectTo("bugzilla.com");
                 LogIn logIn = new LogIn("username", "password");
                 conn.executeMethod(logIn);

                 Bug bug = new BugFactory()
                .newBug()
                .setProduct("SeleniumFramework")
                .setComponent("CoreJavaTestNG")
                 .setVersion("1.0").setPlatform("PC")
                 .setOperatingSystem("Windows")
                 .setDescription("Bug posted from Java Source Code")
                 .setSummary("Bug posted from Java Source Code")
                 .createBug();

                 ReportBug report = new ReportBug(bug);
                 conn.executeMethod(report);
                 int bugID = report.getID();
                 System.out.println("Bug posted and its ID is " + bugID);
                 GetBug get = new GetBug(bugID);
                 conn.executeMethod(get);

                 System.out.println(get.getBug().getID());
                 System.out.println(get.getBug().getSummary());
                 System.out.println(get.getBug().getProduct());
                 System.out.println(get.getBug().getComponent());
                 System.out.println(get.getBug().getVersion());
                 System.out.println(get.getBug().getPlatform());
                 System.out.println(get.getBug().getOperatingSystem());

            // Passing txtFileFilter to listFiles() method to retrieve only file start with fail files
            File[] files = folder.listFiles(txtFileFilter);
            int Count = 0;
            for (File file : files) {


                  BufferedImage bImage = ImageIO.read(new File(FilePath + file.getName()));
                  ByteArrayOutputStream bos = new ByteArrayOutputStream();
                  ImageIO.write(bImage, "jpg", bos );
                  byte [] data = bos.toByteArray();

                             AttachmentFactory attachmentFactory = new AttachmentFactory();
                             Attachment attachment = attachmentFactory.newAttachment()
                           . setData(data)
                           . setMime("image/jpg") //Set the appropriate MIME type for the image format
                           . setSummary(file.toString()) //Description
                           . setName(file.toString())//Name of the Screenshot in Bugzilla
                           . setBugID(bugID)
                           . createAttachment();

                            AddAttachment add2 = new AddAttachment(attachment, bugID);
                            add2.getID();
                            conn.executeMethod(add2);                    
            Count++;

            }
            System.out.println(Count + "  File Uploded");

             }
            catch (Exception e) {
            e.printStackTrace();
            } ```

  [1]: https://i.stack.imgur.com/qrIaq.jpg

共 (1) 个答案

  1. # 1 楼答案

    你看到的粉红色/浅红色是因为源图像包含一个alpha通道

    ImageIO中有一个已知的bug,它会将alpha通道包含到JPEG图像的输出中(或者类似的东西,如果你真的感兴趣,可以用谷歌搜索)

    问题的基本解决方案是使用TYPE_INT_RGB将原始图像应用于BufferedImage,这将删除alpha通道,例如请参见Removing transparency in PNG BufferedImage

    I used the code but am getting blue color background on the image

    所以,从这个透明的PNG开始

    Original image

    使用下面的代码

    BufferedImage original = ImageIO.read(new File("/Users/shanew/Downloads/transparent.png"));
    
    BufferedImage copy = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = copy.createGraphics();
    g2d.setColor(Color.WHITE); // Or what ever fill color you want...
    g2d.fillRect(0, 0, copy.getWidth(), copy.getHeight());
    g2d.drawImage(original, 0, 0, null);
    g2d.dispose();
    
    File dest = new File("Test.jpg");
    ImageIO.write(copy, "jpg", dest);
    
    BufferedImage test = ImageIO.read(dest);
    
    JPanel panel = new JPanel();
    panel.add(new JLabel(new ImageIcon(original)));
    panel.add(new JLabel(new ImageIcon(test)));
    
    JOptionPane.showMessageDialog(null, panel);
    

    我可以生产

    Output

    如果您仍然有问题,那么您需要做两件事:

    1. 用您正在使用的代码更新您的原始问题
    2. 提供您尝试转换的图像的示例

    在评论中持续发布代码是没有帮助的