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

Sum v1.1bugfix #279

Merged
merged 3 commits into from
Jan 21, 2021
Merged
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
4 changes: 4 additions & 0 deletions classifai-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
</dependency>
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import ai.classifai.util.ParamConfig;
import ai.classifai.util.ProjectHandler;
import ai.classifai.util.type.AnnotationType;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -100,6 +104,66 @@ public static boolean isImageReadable(String imagePath)

}

private static int getExifOrientation(File file){
try
{
Metadata metadata = JpegMetadataReader.readMetadata(file);
Directory dir= metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);

return dir.getInt(274);
}
catch(Exception e)
{
return 0;
}
}

private static BufferedImage rotate(BufferedImage image, double angle){

double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));

int w = image.getWidth();
int h = image.getHeight();

int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);

int type = image.getType();
BufferedImage result = new BufferedImage(newW, newH, type);

Graphics2D g = result.createGraphics();

g.translate((newW - w) / 2, (newH - h) / 2);
g.rotate(angle,((double)w) / 2, ((double)h) / 2);
g.drawRenderedImage(image, null);

return result;
}

private static BufferedImage rotateWithOrientation(BufferedImage img, int orientation){
double angle = 0;
if (orientation == 8) angle = -Math.PI/2;
else if (orientation == 3) angle = Math.PI;
else if (orientation == 6) angle = Math.PI/2;

return rotate(img,angle);
}

private static int getHeight(BufferedImage img, int orientation){
if (orientation == 8 || orientation == 6){
return img.getWidth();
}
return img.getHeight();
}

private static int getWidth(BufferedImage img, int orientation){
if (orientation == 8 || orientation == 6){
return img.getHeight();
}
return img.getWidth();
}


public static Map<String, String> getThumbNail(String imageAbsPath)
{
Expand All @@ -108,9 +172,13 @@ public static Map<String, String> getThumbNail(String imageAbsPath)
File file = new File(imageAbsPath);

BufferedImage img = ImageIO.read(file);
int orientation = getExifOrientation(file);

Integer oriHeight = getHeight(img, orientation);
Integer oriWidth = getWidth(img, orientation);

Integer oriHeight = img.getHeight();
Integer oriWidth = img.getWidth();
//rotate for thumbnail generation
img = rotateWithOrientation(img, orientation);

int type = img.getColorModel().getColorSpace().getType();
boolean grayscale = (type == ColorSpace.TYPE_GRAY || type == ColorSpace.CS_GRAY);
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>3.2.4</maven-shade-plugin.version>
<apache.commons.version>3.8.1</apache.commons.version>
<metadata.extractor.version>2.15.0</metadata.extractor.version>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<classifai.release.version>1.1.0</classifai.release.version>
Expand Down Expand Up @@ -212,6 +213,11 @@
<artifactId>jai-imageio-jpeg2000</artifactId>
<version>${jai.imageio.version}</version>
</dependency>
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>${metadata.extractor.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down