diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java index e2bf68b..2fd1ce0 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java @@ -148,7 +148,7 @@ public class VotingBoothConfigurationCreator { return ByteString.copyFromUtf8(value.getString("answer")); } if (questionType == BallotQuestionUIElementOuterClass.ValueType.IMAGE_TYPE.getNumber()) { - return ByteString.copyFrom(getFileBytes(value.getString("answer"))); + return ByteString.copyFrom(getImageBytes(value.getString("answer"))); } else { throw new IOException(UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE); @@ -156,14 +156,20 @@ public class VotingBoothConfigurationCreator { } /** - * Converts the image from given path to bytes array - * @param filepath the path to the wanted picture + * Converts the image to bytes array + * @param fileOrResourcePath the path to the wanted picture. Tries to read as a file path, and if not successful as a resource path. * @return byte array of the image - * @throws IOException will be thrown if the given file doesnt exist + * @throws IOException will be thrown if the given file doesn't exist and there is no resource of that path either. */ - private byte[] getFileBytes(String filepath) throws IOException { + private byte[] getImageBytes(String fileOrResourcePath) throws IOException { byte[] imageInByte; - BufferedImage originalImage = ImageIO.read(new File(filepath)); + File imageFile = new File(fileOrResourcePath); + BufferedImage originalImage; + if (imageFile.exists()) { + originalImage = ImageIO.read(new File(fileOrResourcePath)); + } else { + originalImage = ImageIO.read(getClass().getResourceAsStream(fileOrResourcePath)); + } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(originalImage, IMAGES_TYPE, baos); diff --git a/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json b/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json index fe1c33c..d3bd1ce 100644 --- a/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json +++ b/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json @@ -34,15 +34,15 @@ "type": 2, "answersValue": [ { - "answer": "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/GeorgeBush.png", + "answer": "/images/GeorgeBush.png", "description": "George Boosh image." }, { - "answer": "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/MichaelJackson.png", + "answer": "/images/MichaelJackson.png", "description": "Michael Jakson image." }, { - "answer": "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/AntonioBanderass.png", + "answer": "/images/AntonioBanderass.png", "description": "Antonio Banderass image." } ]