java - 捕获的帧在服务器中向右旋转 90 度

标签 java android image-rotation jcodec

从 Android 手机发送的视频中捕获帧或图像,使用 Jcodec(<artifactId>jcodec</artifactId><artifactId>jcodec-javase</artifactId>Version 0.2.2)在java中捕获图像。一切正常,但显示照片时向右倾斜 90 度。我无法发现在捕获帧或显示帧时发生旋转!

在本地服务器(tomcat7)中工作正常(图像本身是肖像),但是当我将代码推送到AWS时,它有tomcat8,出现了这个问题。下载后,来自 AWS 的图像(JPEG)大小为 28kb,来自本地服务器的图像大小(JPEG)为 118kb。

我在这里分享我的代码,任何人请告诉我哪里出了问题,任何解决此问题的链接都将非常有用。

帧捕获代码:

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

图像显示代码:

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

最佳答案

在android中,您可以使用ExifInterface来确定图像/视频是否有旋转。

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;
}

关于java - 捕获的帧在服务器中向右旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49485736/

相关文章:

java - 模拟 ReactiveSecurityContextHolder

java - Android Picasso 不同的错误

android - 在下面的类中实现带屏幕限制的 Imageview 旋转

java - 使用 onTouchListener 旋转图像而不缩放图像

Java - 当 "this"是唯一的出路时?

java - 如果父对象不是单例,那么子对象也是单例吗?

java - 原生广告静音不显示的原因?

python - 有没有简单的方法来旋转矩阵/数组的值?

Java Web 服务请求-响应问题

java - android – 在有限的时间内显示一个按钮