简体   繁体   中英

How can I get the actual color of the image while uploading to Bugzilla tool using java code with out any distortion?

**I'm using the below code to fetch the multiple failure screenshots from the folder to Bugzilla tool, while uploading the pictures in bugzilla, color of the picture is disorted. [enter image description here][1]. Can any one help me to rectify this issue. ? **

             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

The pinkish/readish ting your seeing is because the source image contains a alpha channel.

There is a known bug in ImageIO which will include the alpha channel into the output of the JPEG image (or some such thing, you can google it if you're really interested).

The basic solution to your problem is to apply the original image to a BufferedImage using a TYPE_INT_RGB , which will remove the alpha channel, for example see Removing transparency in PNG BufferedImage .

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

So, starting with this transparent PNG

原始图像

And using the below code...

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

I can produce...

输出

If you're still having issues, then you need to do two things:

  1. Update your original question with the code you are using
  2. Provide a sample of the image you are trying to convert

It's not helpful to keep posting code in the comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM