<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>xử lý ảnh - HaiNH</title>
	<atom:link href="https://hainh.dev/tag/xu-ly-anh/feed/" rel="self" type="application/rss+xml" />
	<link>https://hainh.dev</link>
	<description></description>
	<lastBuildDate>Wed, 08 May 2019 09:40:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://hainh.dev/wp-content/uploads/2025/10/cropped-logomyblog-32x32.png</url>
	<title>xử lý ảnh - HaiNH</title>
	<link>https://hainh.dev</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">249252746</site>	<item>
		<title>Vấn đề khi upload hình ảnh từ điện thoại lên server bị xoay hình</title>
		<link>https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh</link>
					<comments>https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/#respond</comments>
		
		<dc:creator><![CDATA[NGUYỄN HOÀNG HẢI]]></dc:creator>
		<pubDate>Wed, 08 May 2019 09:40:22 +0000</pubDate>
				<category><![CDATA[Lập trình ứng dụng]]></category>
		<category><![CDATA[điện thoại]]></category>
		<category><![CDATA[lập trình ứng dụng]]></category>
		<category><![CDATA[xử lý ảnh]]></category>
		<guid isPermaLink="false">https://hainh2k3.com/?p=2636</guid>

					<description><![CDATA[<p>Chẳng là gần đây mình có một module thực hiện việc upload hình ảnh từ mobile lên server, tuy nhiên khi hình ảnh được đẩy lên server rồi thì xem lại có hình thì bị xoay, hình thì không như dưới đây Sau khi loay hoay tìm các vấn đề không biết tại sao thì...</p>
<p>The post <a href="https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/">Vấn đề khi upload hình ảnh từ điện thoại lên server bị xoay hình</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Chẳng là gần đây mình có một module thực hiện việc upload hình ảnh từ mobile lên server, tuy nhiên khi hình ảnh được đẩy lên server rồi thì xem lại có hình thì bị xoay, hình thì không như dưới đây</p>
<p><img fetchpriority="high" decoding="async" data-attachment-id="2637" data-permalink="https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/mobile-upload-hinh-anh-bi-xoay/" data-orig-file="https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay.jpg" data-orig-size="1115,662" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="mobile upload hinh anh bi xoay" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay-300x178.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay-1024x608.jpg" class="aligncenter size-full wp-image-2637" src="https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay.jpg" alt="Hình ảnh bị xoay khi upload lên server được chụp bằng điện thoại" width="1115" height="662" srcset="https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay.jpg 1115w, https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay-300x178.jpg 300w, https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay-1024x608.jpg 1024w, https://hainh.dev/wp-content/uploads/2019/05/mobile-upload-hinh-anh-bi-xoay-768x456.jpg 768w" sizes="(max-width: 1115px) 100vw, 1115px" /></p>
<p>Sau khi loay hoay tìm các vấn đề không biết tại sao thì cuối cùng mình cũng tìm ra từ khóa &#8220;mobile image exif orientation&#8221;. Và cuối cùng vấn đề của mình cũng đã được giải quyết. Lý do là vì bất kỳ hình ảnh nào được chụp từ thiết bị di động sẽ đều được lưu hướng gia tốc ( xoay ) hiện tại của điện thoại vào thẻ Exif, gọi là metadata image. Các bạn có thể tìm hiểu thêm thông tin về Exif <a href="https://en.wikipedia.org/wiki/Exif" target="_blank" rel="noopener noreferrer">tại đây</a>  . Dựa vào thông tin được lưu trong ảnh đó thì các trình xem ảnh sẽ tự động xoay hướng ảnh sao cho phù hợp. Vấn đề là khi chúng ta upload lên server và chương trình xem ảnh của chúng ta lại không quan tâm đến thông tin đó, bởi vậy ảnh bị hiển thi sai hướng. Giải quyết vấn đề này chúng ta có 2 hướng:</p>
<ol>
<li>Đọc thông tin đó và xoay lại hướng phù hợp trước khi ra view người xem.</li>
<li>Xoay lại hướng trước khi lưu vào server.</li>
</ol>
<h3><span style="color: #339966;">Giá trị được lưu vào EXIF orientation sẽ là kiểu số từ 1 đến 8:</span></h3>
<ol>
<li>0 độ, không cần điều chỉnh</li>
<li>0 độ, hình ảnh được lật về bên phải</li>
<li>180 độ, ảnh lật xuống phía dưới</li>
<li>180 độ, ảnh lật về phía phải</li>
<li>90 độ, ảnh lật về phía phải</li>
<li>90 độ, quay phải</li>
<li>270 độ, ảnh lật về phía phải</li>
<li>270 độ, quay phải</li>
</ol>
<p><img decoding="async" data-attachment-id="2638" data-permalink="https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/exif_orientations-1/" data-orig-file="https://hainh.dev/wp-content/uploads/2019/05/EXIF_Orientations-1.gif" data-orig-size="512,252" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="EXIF_Orientations (1)" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2019/05/EXIF_Orientations-1-300x148.gif" data-large-file="https://hainh.dev/wp-content/uploads/2019/05/EXIF_Orientations-1.gif" class="size-full wp-image-2638 alignnone" src="https://hainh.dev/wp-content/uploads/2019/05/EXIF_Orientations-1.gif" alt="" width="512" height="252" /></p>
<p>Về vấn để xử lý trên code C# với đối tượng Image hoặc Bitmap thì đầu tiên mình lấy giá trị Exif Orientation của nó, sau đó giựa vào giá trị từ 1 &#8211; 8 đó để biết được hướng ảnh cần xoay, cuối cùng gọi phương thức RotateFlip của ảnh đó là xong.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="rowhammer">// Biến img là biến Image hình ảnh
// 0x0112 là tag Orientation được lưu trong Exif
var _exifOrientation = img.GetPropertyItem(0x0112); // return 1 -&gt; 8
// Để chắc ăn là hình ảnh có tag đó thì ta có thể dùng lệnh kiểm tra
if (img.PropertyIdList.Contains(0x0112)){ .... }</pre>
<p>Đoạn code lấy hướng cần xoay của ảnh giựa vào giá trị Exif Orientation</p>
<pre class="EnlighterJSRAW" data-enlighter-theme="rowhammer" data-enlighter-language="csharp">private RotateFlipType GetOrientationToFlipType(int orientationValue)
{
  RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone;
  switch (orientationValue)
  {
    case 1:
      rotateFlipType = RotateFlipType.RotateNoneFlipNone;
      break;
    case 2:
      rotateFlipType = RotateFlipType.RotateNoneFlipX;
      break;
    case 3:
      rotateFlipType = RotateFlipType.Rotate180FlipNone;
      break;
    case 4:
      rotateFlipType = RotateFlipType.Rotate180FlipX;
      break;
    case 5:
      rotateFlipType = RotateFlipType.Rotate90FlipX;
      break;
    case 6:
      rotateFlipType = RotateFlipType.Rotate90FlipNone;
      break;
    case 7:
      rotateFlipType = RotateFlipType.Rotate270FlipX;
      break;
    case 8:
      rotateFlipType = RotateFlipType.Rotate270FlipNone;
      break;
    default:
      rotateFlipType = RotateFlipType.RotateNoneFlipNone;
      break;
  }
  return rotateFlipType;
}</pre>
<p>Xoay lại hình ảnh theo hướng cần thiết</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="rowhammer">img.RotateFlip(GetOrientationToFlipType(_exifOrientation));</pre><p>The post <a href="https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/">Vấn đề khi upload hình ảnh từ điện thoại lên server bị xoay hình</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://hainh.dev/van-de-khi-upload-hinh-anh-tu-dien-thoai-len-server-bi-xoay-hinh/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2636</post-id>	</item>
		<item>
		<title>Phần mềm phát hiện vị trí Container ISO</title>
		<link>https://hainh.dev/phan-mem-phat-hien-vi-tri-container-iso/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=phan-mem-phat-hien-vi-tri-container-iso</link>
					<comments>https://hainh.dev/phan-mem-phat-hien-vi-tri-container-iso/#comments</comments>
		
		<dc:creator><![CDATA[NGUYỄN HOÀNG HẢI]]></dc:creator>
		<pubDate>Wed, 24 Oct 2018 04:30:10 +0000</pubDate>
				<category><![CDATA[Dự án]]></category>
		<category><![CDATA[công cụ]]></category>
		<category><![CDATA[dự án]]></category>
		<category><![CDATA[opencv]]></category>
		<category><![CDATA[phần mềm]]></category>
		<category><![CDATA[xử lý ảnh]]></category>
		<guid isPermaLink="false">https://hainh2k3.com/?p=581</guid>

					<description><![CDATA[<p>Đây là chương trình phát hiện vị trí số Container, là bước đầu để mình tiến hành việc đọc số container. Chương trình mình viết bằng Java, OpenCV để xử lý ảnh. Chương trình mình viết hồi năm 2013, rất tiếc là không theo được đến cùng vì không ứng dụng được tiếp. [su_youtube url=&#8221;https://www.youtube.com/watch?v=OxjhHYc8mzI&#38;t=69s&#8221;]...</p>
<p>The post <a href="https://hainh.dev/phan-mem-phat-hien-vi-tri-container-iso/">Phần mềm phát hiện vị trí Container ISO</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Đây là chương trình phát hiện vị trí số Container, là bước đầu để mình tiến hành việc đọc số container. Chương trình mình viết bằng Java, OpenCV để xử lý ảnh. Chương trình mình viết hồi năm 2013, rất tiếc là không theo được đến cùng vì không ứng dụng được tiếp.</p>
<p><img decoding="async" class="aligncenter size-full wp-image-582" src="https://hainh.dev/wp-content/uploads/2018/10/kiem-tra-vi-tri-so-cont.jpg" alt="" width="1184" height="667" /></p>
<p>[su_youtube url=&#8221;https://www.youtube.com/watch?v=OxjhHYc8mzI&amp;t=69s&#8221;]</p>
<p>Ngoài viết bằng Java ra thì mình có viết bằng .NET, các thuật toán xử lý ảnh đều do mình viết mà không cần thư viện khác. Các bạn có thể tham khảo demo ở đây : <a href="http://occr.apphb.com">http://occr.apphb.com</a></p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-583" src="https://hainh.dev/wp-content/uploads/2018/10/kiem-tra-vi-tri-so-cont-2.jpg" alt="" width="871" height="2993" /></p>


<p>Xem thêm phiên bản mới phát hiện và đọc số container bằng camera : <a href="https://hainh.dev/phat-hien-va-doc-so-container-bang-camera/">Phát hiện và đọc số container bằng camera – HaiNH&apos;s Blog (hainh.dev/)</a></p><p>The post <a href="https://hainh.dev/phan-mem-phat-hien-vi-tri-container-iso/">Phần mềm phát hiện vị trí Container ISO</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://hainh.dev/phan-mem-phat-hien-vi-tri-container-iso/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4723</post-id>	</item>
	</channel>
</rss>
