在使用不带任何失真的Java代码上传到Bugzilla工具时,如何获得图像的实际颜色?

问题描述 投票:0回答:1

**我正在使用以下代码将文件夹中的多个失败屏幕截图从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
java jpeg javax.imageio bugzilla
1个回答
0
投票

您看到的粉红色/淡淡的色彩是因为源图像包含一个Alpha通道。

ImageIO中有一个已知的错误,它将在JPEG图像的输出中包含alpha通道(或某些类似的东西,如果您真的有兴趣,可以在Google上对其进行搜索。)>]

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

我使用了代码,但是图像上出现了蓝色背景

所以,从此透明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. 提供要转换的图像样本
  3. 在评论中保持张贴代码没有帮助

© www.soinside.com 2019 - 2024. All rights reserved.