Allow using resource paths for images linked in the JSON configuration file
parent
ac4d668984
commit
c4e0e5f56d
|
@ -148,7 +148,7 @@ public class VotingBoothConfigurationCreator {
|
||||||
return ByteString.copyFromUtf8(value.getString("answer"));
|
return ByteString.copyFromUtf8(value.getString("answer"));
|
||||||
}
|
}
|
||||||
if (questionType == BallotQuestionUIElementOuterClass.ValueType.IMAGE_TYPE.getNumber()) {
|
if (questionType == BallotQuestionUIElementOuterClass.ValueType.IMAGE_TYPE.getNumber()) {
|
||||||
return ByteString.copyFrom(getFileBytes(value.getString("answer")));
|
return ByteString.copyFrom(getImageBytes(value.getString("answer")));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE);
|
throw new IOException(UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE);
|
||||||
|
@ -156,14 +156,20 @@ public class VotingBoothConfigurationCreator {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the image from given path to bytes array
|
* Converts the image to bytes array
|
||||||
* @param filepath the path to the wanted picture
|
* @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
|
* @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;
|
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();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
ImageIO.write(originalImage, IMAGES_TYPE, baos);
|
ImageIO.write(originalImage, IMAGES_TYPE, baos);
|
||||||
|
|
|
@ -34,15 +34,15 @@
|
||||||
"type": 2,
|
"type": 2,
|
||||||
"answersValue": [
|
"answersValue": [
|
||||||
{
|
{
|
||||||
"answer": "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/GeorgeBush.png",
|
"answer": "/images/GeorgeBush.png",
|
||||||
"description": "George Boosh image."
|
"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."
|
"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."
|
"description": "Antonio Banderass image."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue