Allow using resource paths for images linked in the JSON configuration file

android-scanner
Tal Moran 2016-10-30 15:34:58 +02:00
parent ac4d668984
commit c4e0e5f56d
2 changed files with 15 additions and 9 deletions

View File

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

View File

@ -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."
}
]