<tfoot id='xMkKo'></tfoot>
      <bdo id='xMkKo'></bdo><ul id='xMkKo'></ul>

  • <small id='xMkKo'></small><noframes id='xMkKo'>

        <legend id='xMkKo'><style id='xMkKo'><dir id='xMkKo'><q id='xMkKo'></q></dir></style></legend>
      1. <i id='xMkKo'><tr id='xMkKo'><dt id='xMkKo'><q id='xMkKo'><span id='xMkKo'><b id='xMkKo'><form id='xMkKo'><ins id='xMkKo'></ins><ul id='xMkKo'></ul><sub id='xMkKo'></sub></form><legend id='xMkKo'></legend><bdo id='xMkKo'><pre id='xMkKo'><center id='xMkKo'></center></pre></bdo></b><th id='xMkKo'></th></span></q></dt></tr></i><div id='xMkKo'><tfoot id='xMkKo'></tfoot><dl id='xMkKo'><fieldset id='xMkKo'></fieldset></dl></div>

        在客户端的 JavaScript 中访问 JPEG EXIF 旋转数据

        时间:2023-06-21
            <bdo id='tvBwt'></bdo><ul id='tvBwt'></ul>
            <legend id='tvBwt'><style id='tvBwt'><dir id='tvBwt'><q id='tvBwt'></q></dir></style></legend>

            <small id='tvBwt'></small><noframes id='tvBwt'>

          • <tfoot id='tvBwt'></tfoot>
            • <i id='tvBwt'><tr id='tvBwt'><dt id='tvBwt'><q id='tvBwt'><span id='tvBwt'><b id='tvBwt'><form id='tvBwt'><ins id='tvBwt'></ins><ul id='tvBwt'></ul><sub id='tvBwt'></sub></form><legend id='tvBwt'></legend><bdo id='tvBwt'><pre id='tvBwt'><center id='tvBwt'></center></pre></bdo></b><th id='tvBwt'></th></span></q></dt></tr></i><div id='tvBwt'><tfoot id='tvBwt'></tfoot><dl id='tvBwt'><fieldset id='tvBwt'></fieldset></dl></div>
                <tbody id='tvBwt'></tbody>

                1. 本文介绍了在客户端的 JavaScript 中访问 JPEG EXIF 旋转数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想根据相机在 JPEG EXIF 图像数据中设置的原始旋转来旋转照片.诀窍是所有这些都应该发生在浏览器中,使用 JavaScript 和 <canvas>.

                  I'd like to rotate photos based on their original rotation, as set by the camera in JPEG EXIF image data. The trick is that all this should happen in the browser, using JavaScript and <canvas>.

                  JavaScript如何访问JPEG、本地文件API对象、本地<img>或远程<img>、EXIF数据来读取旋转信息?

                  How could JavaScript access JPEG, a local file API object, local <img> or remote <img>, EXIF data to read the rotation information?

                  服务器端的答案不行;我正在寻找客户端解决方案.

                  Server-side answers are not OK; I am looking for a client-side solution.

                  推荐答案

                  如果你只想要方向标签而不想要别的并且不喜欢包含另一个巨大的 javascript 库,我写了一个小代码,可以快速提取方向标签尽可能(它使用 IE10+ 中可用的 DataView 和 readAsArrayBuffer,但您可以为旧浏览器编写自己的数据阅读器):

                  If you only want the orientation tag and nothing else and don't like to include another huge javascript library I wrote a little code that extracts the orientation tag as fast as possible (It uses DataView and readAsArrayBuffer which are available in IE10+, but you can write your own data reader for older browsers):

                  function getOrientation(file, callback) {
                      var reader = new FileReader();
                      reader.onload = function(e) {
                  
                          var view = new DataView(e.target.result);
                          if (view.getUint16(0, false) != 0xFFD8)
                          {
                              return callback(-2);
                          }
                          var length = view.byteLength, offset = 2;
                          while (offset < length) 
                          {
                              if (view.getUint16(offset+2, false) <= 8) return callback(-1);
                              var marker = view.getUint16(offset, false);
                              offset += 2;
                              if (marker == 0xFFE1) 
                              {
                                  if (view.getUint32(offset += 2, false) != 0x45786966) 
                                  {
                                      return callback(-1);
                                  }
                  
                                  var little = view.getUint16(offset += 6, false) == 0x4949;
                                  offset += view.getUint32(offset + 4, little);
                                  var tags = view.getUint16(offset, little);
                                  offset += 2;
                                  for (var i = 0; i < tags; i++)
                                  {
                                      if (view.getUint16(offset + (i * 12), little) == 0x0112)
                                      {
                                          return callback(view.getUint16(offset + (i * 12) + 8, little));
                                      }
                                  }
                              }
                              else if ((marker & 0xFF00) != 0xFF00)
                              {
                                  break;
                              }
                              else
                              { 
                                  offset += view.getUint16(offset, false);
                              }
                          }
                          return callback(-1);
                      };
                      reader.readAsArrayBuffer(file);
                  }
                  
                  // usage:
                  var input = document.getElementById('input');
                  input.onchange = function(e) {
                      getOrientation(input.files[0], function(orientation) {
                          alert('orientation: ' + orientation);
                      });
                  }

                  <input id='input' type='file' />

                  价值观:

                  -2: not jpeg
                  -1: not defined
                  

                  对于那些使用 Typescript 的人,你可以使用以下代码:

                  For those using Typescript, you can use the following code:

                  export const getOrientation = (file: File, callback: Function) => {
                    var reader = new FileReader();
                  
                    reader.onload = (event: ProgressEvent) => {
                  
                      if (! event.target) {
                        return;
                      }
                  
                      const file = event.target as FileReader;
                      const view = new DataView(file.result as ArrayBuffer);
                  
                      if (view.getUint16(0, false) != 0xFFD8) {
                          return callback(-2);
                      }
                  
                      const length = view.byteLength
                      let offset = 2;
                  
                      while (offset < length)
                      {
                          if (view.getUint16(offset+2, false) <= 8) return callback(-1);
                          let marker = view.getUint16(offset, false);
                          offset += 2;
                  
                          if (marker == 0xFFE1) {
                            if (view.getUint32(offset += 2, false) != 0x45786966) {
                              return callback(-1);
                            }
                  
                            let little = view.getUint16(offset += 6, false) == 0x4949;
                            offset += view.getUint32(offset + 4, little);
                            let tags = view.getUint16(offset, little);
                            offset += 2;
                            for (let i = 0; i < tags; i++) {
                              if (view.getUint16(offset + (i * 12), little) == 0x0112) {
                                return callback(view.getUint16(offset + (i * 12) + 8, little));
                              }
                            }
                          } else if ((marker & 0xFF00) != 0xFF00) {
                              break;
                          }
                          else {
                              offset += view.getUint16(offset, false);
                          }
                      }
                      return callback(-1);
                    };
                  
                    reader.readAsArrayBuffer(file);
                  }
                  

                  这篇关于在客户端的 JavaScript 中访问 JPEG EXIF 旋转数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Html5画布drawImage:如何应用抗锯齿 下一篇:调整 HTML5 画布大小以适应窗口

                  相关文章

                  <i id='Fe89W'><tr id='Fe89W'><dt id='Fe89W'><q id='Fe89W'><span id='Fe89W'><b id='Fe89W'><form id='Fe89W'><ins id='Fe89W'></ins><ul id='Fe89W'></ul><sub id='Fe89W'></sub></form><legend id='Fe89W'></legend><bdo id='Fe89W'><pre id='Fe89W'><center id='Fe89W'></center></pre></bdo></b><th id='Fe89W'></th></span></q></dt></tr></i><div id='Fe89W'><tfoot id='Fe89W'></tfoot><dl id='Fe89W'><fieldset id='Fe89W'></fieldset></dl></div>
                    <bdo id='Fe89W'></bdo><ul id='Fe89W'></ul>
                    <tfoot id='Fe89W'></tfoot>

                    <legend id='Fe89W'><style id='Fe89W'><dir id='Fe89W'><q id='Fe89W'></q></dir></style></legend>

                    <small id='Fe89W'></small><noframes id='Fe89W'>