title: Can't get encoded image out of cv::imencode (getting mess). How to fix broken saving of .jpg?
body: <p>So I try to save image into <code>ostream</code> via <a href="http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html" rel="nofollow noreferrer"><code>cv::imencode</code> apis</a>. From <code>imencode</code> we get vector. As <a href="https://stackoverflow.com/questions/8143090/how-to-save-stdvectoruchar-into-stdostream/8143107#8143107">shown here</a> it can be stored into any ostream. For example <code>std::ofstream</code>. But it fails currupting data=(</p>
  
  <p>This is what we see:</p>
  
  <p><img src="https://i.stack.imgur.com/yfbIi.jpg" alt="enter image description here"></p>
  
  <p>Here is what we get in file:</p>
  
  <p><img src="https://i.stack.imgur.com/or0qq.jpg" alt="enter image description here"></p>
  
  <p>And here is our code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;map&gt;
  #include &lt;string&gt;
  #include &lt;ctime&gt;
  #include &lt;iterator&gt;
  
  #include &lt;opencv2/opencv.hpp&gt;
  
  
  
  void send_data(std::ostream &amp; o, const std::vector&lt;uchar&gt; &amp; v)
  {
      o.write(reinterpret_cast&lt;const char*&gt;(v.data()), v.size());
  }
  
  int main(  )
  {
      int w=400, h=400;
      cv::Mat black = cv::Mat::zeros(cv::Size(w, h), CV_8UC3);
  
      {
          for(int i = 0; i &lt;= 35; ++i)
          {
              for (int k = 0; k &lt;=35; ++k)
              {
                  black.row(i).col(k) = 255;
              }
          }
  
          for(int i = 10; i &lt;= 15; ++i)
          {
              for (int k = 10; k &lt;=15; ++k)
              {
                  black.row(i).col(k) = 0;
              }
          }
  
          for(int i = 25; i &lt;= 30; ++i)
          {
              for (int k = 25; k &lt;=30; ++k)
              {
                  black.row(i).col(k) = 0;
              }
          }
  
          for(int i = 25; i &lt;= 75; ++i)
          {
              for (int k = 25; k &lt;= 75; ++k)
              {
                  black.row(i).col(k) = 255;
              }
          }
  
          for(int i = 35; i &lt;= 65; ++i)
          {
              for (int k = 35; k &lt;=65; ++k)
              {
                  black.row(i).col(k) = 0;
              }
          }
  
          for(int i = 57; i &lt;= 62; ++i)
          {
              for (int k = 57; k &lt;=62; ++k)
              {
                  black.row(i).col(k) = 255;
              }
          }
  
          for(int i = 90; i &lt;= 99; ++i)
          {
              for (int k = 90; k &lt;=99; ++k)
              {
                  black.row(i).col(k) = 255;
              }
          }
      }
  
  
      cv::namedWindow( "Components", CV_WINDOW_AUTOSIZE );
      cv::imshow( "Components", black );
  
      std::vector&lt;uchar&gt; buff;
  
      std::vector&lt;int&gt; p;
      p.push_back(CV_IMWRITE_JPEG_QUALITY);
      p.push_back(9);
  
      cv::imencode(".jpg", black, buff);
  
      std::ofstream outfile ("test.jpg");
      send_data(outfile, buff);
      outfile.close();
  
      cv::waitKey(0);
      std::cin.get();
      return 0;
  }
  </code></pre>