Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATT-1: Enable rotation of image attachments, adding the javascript cod… #28

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected ComplexDataHelper getComplexDataHelper() {
/*
* Complex data CRUD - Save (Update)
*/
abstract protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData);
abstract protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData) throws IOException;

public String[] getSupportedViews() {
return supportedViews;
Expand Down Expand Up @@ -232,8 +232,15 @@ final public Obs saveObs(Obs obs) {
}
}

ValueComplex valueComplex = saveComplexData(obs, complexData);
ValueComplex valueComplex = null;
try {
valueComplex = saveComplexData(obs, complexData);
}
catch (IOException e) {
e.printStackTrace();
}
obs.setValueComplex(valueComplex.getValueComplex());
return obs;

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmrs.module.attachments.obs;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -69,7 +71,7 @@ protected boolean deleteComplexData(Obs obs, AttachmentComplexData complexData)
}

@Override
protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData) {
protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData) throws IOException {
int imageHeight = Integer.MAX_VALUE;
int imageWidth = Integer.MAX_VALUE;

Expand All @@ -79,6 +81,42 @@ protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexDat
File savedFile = AbstractHandler.getComplexDataFile(obs);
String savedFileName = savedFile.getName();

String fileName = complexData.getTitle();
BufferedImage Obs = null;

try {
Obs = ImageIO.read(new File(fileName));
imageHeight = Obs.getHeight();
imageWidth = Obs.getWidth();
}
catch (IOException e) {
e.printStackTrace();
}

if (obs.getValueModifier().equals("instructions.rotate-right")) {
// rotate the image provided in complex data
final double rads = Math.toRadians(90);
final double sin = Math.abs(Math.sin(rads));
final double cos = Math.abs(Math.cos(rads));
imageHeight = (int) Math.floor(Obs.getHeight() * cos + Obs.getWidth() * sin);
imageWidth = (int) Math.floor(Obs.getWidth() * cos + Obs.getHeight() * sin);
final BufferedImage rotatedImage = new BufferedImage(imageWidth, imageHeight, Obs.getType());

final AffineTransform at = new AffineTransform();
at.translate(imageWidth / 2, imageHeight / 2);
at.rotate(rads, 0, 0);
at.translate(-Obs.getWidth() / 2, -Obs.getHeight() / 2);
final AffineTransformOp rotateOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

rotateOp.filter(Obs, rotatedImage);
try {
ImageIO.write(rotatedImage, "obs", new File(savedFileName));
}
catch (IOException e) {
e.printStackTrace();
}
}

// Get image dimensions
try {
BufferedImage image = ImageIO.read(savedFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ angular.module('att.widget.thumbnail')

var saved = Attachment.save({
uuid: $scope.obs.uuid,
comment: $scope.obs.comment
comment: $scope.obs.comment,
valueModifier: "instruction.rotate-right"
});
saved.$promise.then(function(attachment) {
$scope.obs.uuid = attachment.uuid;
Expand Down Expand Up @@ -278,4 +279,4 @@ angular.module('att.widget.thumbnail')
}
}
};
}]);
}]);