Search code examples
javaandroidimage-rotationjcodec

Captured frame is rotating 90 degree right in the Server


Frame or image is captured from the Video sent by the android mobile, Used Jcodec(<artifactId>jcodec</artifactId> and <artifactId>jcodec-javase</artifactId> Version 0.2.2) to capture the image in java. Everything is working fine but while displaying the photo is tilted to right 90 degree. I am not able to find that rotating is happening while capturing the frame or while displaying it!

In local server(tomcat7) working fine(image is in potrait itself) but this issue occured when I push the code to AWS it has tomcat8. AND after downloaded, size of the image(JPEG) from AWS is 28kb, from local server is 118kb.

I am sharing my code here Anyone Please tell me where it is going wrong and any links to solve this issue will be greatful.

Frame Capturing Code:

int frameNumber = 1;
Picture picture = FrameGrab.getFrameFromFile(file, frameNumber);
BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);
ImageIO.write(bufferedImage, "jpg", new File(id + File.separator + fileName + "_" + fileName + ".jpg"));

Image displying code:

public ResponseEntity<Resource> getPhoto(@PathVariable(value = "id") Integer id) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
        String absolutePath = new File(".").getAbsolutePath();
        File file = new File(Paths.get(absolutePath).getParent() + "/" + id);
        if (null != file) {
            FilenameFilter beginswithm = new FilenameFilter() {
             public boolean accept(File directory, String filename) {
                return filename.startsWith("photo");
             }
           };
            File[] files = file.listFiles(beginswithm);
            if (null != files && files.length > 0) {
              Resource resource = null;
              for (final File f : files) {
              headers.set("Content-Disposition", "inline; filename=" + f.getName());
              resource = appContext.getResource("file:"
                              + Paths.get(absolutePath).getParent() + "/" + id + "/" + f.getName());                     
               return new ResponseEntity<>(resource, headers, HttpStatus.OK);
             }
          }
       }
        RecruiterResponseBean resBean = new RecruiterResponseBean();
        resBean.setStatusMessage(Constants.FAILED);
        resBean.setStatusCode(Constants.FAILED_CODE);
        return new ResponseEntity(resBean, HttpStatus.NOT_FOUND);
    }

Solution

  • In android, You can use ExifInterface to find out if the image/video has a rotation.

    ExifInterface exifInterface = new ExifInterface(mFile.getPath());
    int orientation = exifInterface.getAttributeInt(TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        default:
            break;
    }