-1

**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
  • If the images are png (or any other format which supports alpha channel), you'll need to remove the alpha channel first before saving it as JPEG – MadProgrammer Nov 15 '19 at 05:30
  • Am not familiar with the alpha channel , can you please guide me where to include in the code? – Kogul Selvanathan Nov 15 '19 at 05:40
  • [Removing transparency in PNG BufferedImage](https://stackoverflow.com/questions/26918675/removing-transparency-in-png-bufferedimage/26918760#26918760) - You should do this for all the images (unless you can guarantee the actual format) as it won't make any difference to images that don't have a alpha channel to begin with – MadProgrammer Nov 15 '19 at 05:50
  • I used the code but am getting blue color background on the image – Kogul Selvanathan Nov 15 '19 at 06:00
  • Simplify your example. Post the original image and the converted image plus the code you used - don't bother with the upload/bugzilla code for this example, as it's probably not the cause of the issue – MadProgrammer Nov 15 '19 at 06:04
  • `BufferedImage bImage = ImageIO.read(new File(FilePath + file.getName())); BufferedImage copy = new BufferedImage(bImage.getWidth(), bImage.getHeight(), BufferedImage.TYPE_INT_RGB); ByteArrayOutputStream ByteArrayOutputStream = new ByteArrayOutputStream(); Graphics2D g2d = bImage.createGraphics(); g2d.setColor(Color.WHITE); // Or what ever fill color you want... g2d.fillRect(0, 0, copy.getWidth(), copy.getHeight()); g2d.drawImage(copy, 0, 0, null); g2d.dispose()` – Kogul Selvanathan Nov 15 '19 at 06:06
  • You should be painting `bImage` to `copy`, not the other way round – MadProgrammer Nov 15 '19 at 06:06
  • Am getting white back ground image as output. Am not able to see the actual image. – Kogul Selvanathan Nov 15 '19 at 06:12
  • Update your answer with the code you are using. Provide a sample of the original image to test with – MadProgrammer Nov 15 '19 at 06:13
  • ` BufferedImage bImage = ImageIO.read(new File(FilePath + file.getName())); BufferedImage copy = new BufferedImage(bImage.getWidth(), bImage.getHeight(), BufferedImage.TYPE_INT_RGB); ByteArrayOutputStream ByteArrayOutputStream = new ByteArrayOutputStream(); 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(copy, 0, 0, null); g2d.dispose(); ` – Kogul Selvanathan Nov 15 '19 at 06:16
  • Why are you decoding and re-encoding the images in the first place? Wouldn't simply attaching the original image data be better? It would be much faster too... – Harald K Nov 18 '19 at 15:33
  • Am doing because to compress the jpeg image – Kogul Selvanathan Nov 19 '19 at 07:22

1 Answers1

0

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

Original image

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...

Output

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366