출처: https://lists.libav.org/pipermail/libav-user/2010-August/005159.html


Hoang Ong ngochoanghcm at gmail.com 
Fri Aug 6 19:33:28 CEST 2010


  Libavcodec can do it. This is the code from Anand, and his code only 
use ffmpeg libs.

First I also think about libjpeg and found a jpeg library including 
Anroid.mk to port to Android NDK (I'm working in android platform), but 
I search in ffmpeg source code and see that it has the CODEC_ID_JPEG, so 
I think it does not need to use libjpeg.

int WriteJPEG (AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo){
         AVCodecContext         *pOCodecCtx;
         AVCodec                *pOCodec;
         uint8_t                *Buffer;
         int                     BufSiz;
         int                     BufSizActual;
         int                     ImgFmt = PIX_FMT_YUVJ420P; //for the 
newer ffmpeg version, this int to pixelformat
         FILE                   *JPEGFile;
         char                    JPEGFName[256];

         BufSiz = avpicture_get_size ( 
ImgFmt,pCodecCtx->width,pCodecCtx->height );

         Buffer = (uint8_t *)malloc ( BufSiz );
         if ( Buffer == NULL )
                 return ( 0 );
         memset ( Buffer, 0, BufSiz );

         pOCodecCtx = avcodec_alloc_context ( );
         if ( !pOCodecCtx ) {
                  free ( Buffer );
                  return ( 0 );
                  }

         pOCodecCtx->bit_rate      = pCodecCtx->bit_rate;
         pOCodecCtx->width         = pCodecCtx->width;
         pOCodecCtx->height        = pCodecCtx->height;
         pOCodecCtx->pix_fmt       = ImgFmt;
         pOCodecCtx->codec_id      = CODEC_ID_MJPEG;
         pOCodecCtx->codec_type    = CODEC_TYPE_VIDEO;
         pOCodecCtx->time_base.num = pCodecCtx->time_base.num;
         pOCodecCtx->time_base.den = pCodecCtx->time_base.den;

         pOCodec = avcodec_find_encoder ( pOCodecCtx->codec_id );
         if ( !pOCodec ) {
                  free ( Buffer );
                 return ( 0 );
                  }
         if ( avcodec_open ( pOCodecCtx, pOCodec ) < 0 ) {
                  free ( Buffer );
                  return ( 0 );
                 }

                pOCodecCtx->mb_lmin        = pOCodecCtx->lmin = 
pOCodecCtx->qmin * FF_QP2LAMBDA;
         pOCodecCtx->mb_lmax        = pOCodecCtx->lmax = 
pOCodecCtx->qmax * FF_QP2LAMBDA;
         pOCodecCtx->flags          = CODEC_FLAG_QSCALE;
         pOCodecCtx->global_quality = pOCodecCtx->qmin * FF_QP2LAMBDA;

         pFrame->pts     = 1;
         pFrame->quality = pOCodecCtx->global_quality;
         BufSizActual = avcodec_encode_video( 
pOCodecCtx,Buffer,BufSiz,pFrame );

         sprintf ( JPEGFName, "%06d.jpg", FrameNo );
         JPEGFile = fopen ( JPEGFName, "wb" );
         fwrite ( Buffer, 1, BufSizActual, JPEGFile );
         fclose ( JPEGFile );

         avcodec_close ( pOCodecCtx );
         free ( Buffer );
         return ( BufSizActual );
  }

I searched on google and saw that many people ask the same question but 
not found the answer yet. So this will help for developer in the first 
use ffmpeg libs

Hoang Ong

On 8/6/2010 12:20 AM, Michael Chisholm wrote:
> Here we go again... :)  Can't you use libavcodec's builtin mjpeg encoder?
>
> On 8/5/2010 11:38 AM, Glen Ruedinger wrote:
>> You need libjpeg.
>>
>> On Thu, Aug 5, 2010 at 10:58 AM, Hoang Ong<ngochoanghcm at gmail.com>  
>> wrote:
>>
>>>   Hi everybody,
>>>
>>> I can get the AVFrame and output it to a raw image file (.ppm) I try 
>>> to use
>>> sws_scale to convert that image to variety of image type to save it 
>>> as a jpg
>>> file but it not work. I think it not a way to do.
>>>
>>> I dont have much knowledge about image processing. So anybody know 
>>> how to
>>> use ffmpeg's libs to convert AVFrame to jpg please help me. The code 
>>> I used
>>> to save frame as a ppm file like this:
>>>
>>> void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
>>> {
>>>     FILE *pFile;
>>>     char szFilename[32];
>>>     int  y;
>>>
>>>     // Open file
>>>     sprintf(szFilename, "frame.ppm", iFrame);
>>>     pFile=fopen(szFilename, "wb");
>>>     if(pFile==NULL)
>>>         return;
>>>
>>>     // Write header
>>>     fprintf(pFile, "P6\n%d %d\n255\n", width, height);
>>>
>>>     // Write pixel data
>>>     for(y=0; y<height; y++)
>>>         fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, 
>>> pFile);
>>>
>>>     // Close file
>>>     fclose(pFile);
>>> }
>>>
>>> Regards
>>> Hoang Ong
>>> _______________________________________________
>>> libav-user mailing list
>>> libav-user at mplayerhq.hu
>>> https://lists.mplayerhq.hu/mailman/listinfo/libav-user
>>>
>> _______________________________________________
>> libav-user mailing list
>> libav-user at mplayerhq.hu
>> https://lists.mplayerhq.hu/mailman/listinfo/libav-user
>>
>
>
> _______________________________________________
> libav-user mailing list
> libav-user at mplayerhq.hu
> https://lists.mplayerhq.hu/mailman/listinfo/libav-user



More information about the libav-user mailing list

Posted by 세모아
,


Posted by 세모아
,

Visual C++의 .NET 프로그래밍

How to: Parse Strings Using the Split Method (C++/CLI)


Example


// regex_split.cpp

// compile with: /clr

using namespace System;


int main(array<System::String ^> ^args)

//가능함. int main()

{

   String^ delimStr = " ,.:\t";

   Console::WriteLine( "delimiter : '{0}'", delimStr );

   array<Char>^ delimiter = delimStr->ToCharArray( );

   array<String^>^ words;

   String^ line = "one\ttwo three:four,five six seven";


   Console::WriteLine( "text : '{0}'", line );

   words = line->Split( delimiter );

   Console::WriteLine( "Number of Words : {0}", words->Length );

   for (int word=0; word<words->Length; word++)

      Console::WriteLine( "{0}", words[word] );


   return 0;

}

결과

delimiter : ' ,.:       '

text : 'one     two three:four,five six seven'

Number of Words : 7

one

two

three

four

five

six

seven



Posted by 세모아
,
C++/CLI Console 프로그램 코드에서
 MessageBox 사용시 다음 에러 발생하면 해결책은?
 - 에러 : /LNK2028: unresolved token(0A00003D) "extern "C" int __stdcall MessageBoxW .....
 - 해결책 : Edit your project properties, find the Linker Inputs option,
             and add kernel32.lib user32.lib advapi32.lib which are the usual libraries needed by Win32 code.


Posted by 세모아
,

CLR - Windows Forms Application이 VC++2013에는 없음.


참고 : 

이를 해결하는 편법이 인터넷에 있음. 내 Delicious에 저장함

아래가 그 링크임

http://mcn-www.jwu.ac.jp/~yokamoto/openwww/vsg/VCpp2012FormApp/


아래가 위 링크의 내용을 pdf로 저장.

VC++2010



VC++2013


Posted by 세모아
,

VC++6에는 F4/Shift+F4로 찾기 결과창에서 앞뒤로 이동하였는데,

VS2013에서는 F8로 되고,

F4는 Properties 창이 띄우기에 불편하여

손에 익숙하고 이동이 빠른 F4 로 변경하는 방법:


아래 그림의 것을 서로 바꾸어 설정한다.


F4 / Shift+F4


F8/Shift+F8




Posted by 세모아
,

COPYDATASTRUCT 의

cbData에 숫자를, lpData에 문자열만 넣어서 

간단한 자료만 송수신 하는 예제 코드




송신부 (C#)

        public struct COPYDATASTRUCT

        {

            public IntPtr dwData;

            public int cbData;

            [MarshalAs(UnmanagedType.LPStr)]

            public string lpData;

        }


        private void OnButtonSendClick(object sender, EventArgs e)

        {

            string msg = this.tbMsg.Text.Trim();


            if (string.IsNullOrEmpty(msg))

            {

                MessageBox.Show("메세지를 입력해주세요");

                return;

            }


            IntPtr hwndRcvr = FindWindow(null, "CppReceiveWM_COPYDATA"); //"LHJ WINDOW");

//                byte[] buff = System.Text.Encoding.Default.GetBytes(msg);


                COPYDATASTRUCT cds = new COPYDATASTRUCT();

                cds.dwData = IntPtr.Zero;

//                cds.cbData = buff.Length + 1; //+1을 빼면 수신측에서 cds.lpdata의 뒤에 이상한 글자 붙음.

                cds.cbData = msg.Length+1; //+1을 빼면 수신측에서 cds.lpdata의 뒤에 이상한 글자 붙음.

                cds.lpData = msg;


                SendMessage(hwndRcvr, WM_COPYDATA, 0, ref cds);

        }





수신부 (C++)

 : memcpy_s(.) 을 없애서 빠른 응답속도를 지원토록 함


typedef struct tagCOPYDATASTRUCT2 {
    ULONG_PTR dwData;
    DWORD cbData;
     PVOID lpData;
} COPYDATASTRUCT2, *PCOPYDATASTRUCT2;


INT_PTR CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

//LHJ version //15.3.28 ---------------

    switch (message)

    {

case WM_COPYDATA:        

HandleWMCOPYDATA(hWnd, wParam, lParam);

break;

    default:

return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}


bool HandleWMCOPYDATA(HWND hWnd, WPARAM wParam, LPARAM lParam)

{

PCOPYDATASTRUCT2 pcds;

pcds = (PCOPYDATASTRUCT2)lParam;

try 

{

    // If the size matches

int nsize = pcds->cbData;

{

        SetDlgItemInt(hWnd, IDC_NUMBER_STATIC, nsize, TRUE);

//   std::wstring s1 = (LPCWSTR)pcds->lpData; //송신부가  [MarshalAs(UnmanagedType.LPStr)] 이므로 LPCWSTR은 에러


std::string s2 = (LPSTR)pcds->lpData;

std::wstring s1;

s1.assign(s2.begin(), s2.end());  


// SetDlgItemTextW(hWnd, IDC_MESSAGE_STATIC, (LPCWSTR)pcds->lpData); // >image);

SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, s1.c_str()); // >image);




// The receiving application should consider the data (pcds->lpData) 

        // read-only. The pcds parameter is valid only during the processing 

        // of the message. The receiving application should not free the 

        // memory referenced by pcds. If the receiving application must 

        // access the data after SendMessage returns, it must copy the data 

        // into a local buffer. 

//        memcpy_s(&myStruct, sizeof(myStruct), pcds->lpData, pcds->cbData);

//        memcpy_s(&myStruct, nsize, pcds->lpData, pcds->cbData);


        // Display the MY_STRUCT value in the window.

        // Display the MY_STRUCT value in the window.

//        SetDlgItemInt(hWnd, IDC_NUMBER_STATIC, myStruct.Number, TRUE);

//        SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, myStruct.Message);


//wchar_t text1[50000];

//mbstowcs(text1, myStruct.Message, strlen(myStruct.Message) + 1);

//        SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, text1);


//        SetDlgItemInt(hWnd, IDC_NUMBER_STATIC, pMyStruct->Number, TRUE);

// LPCTSTR s2;

// s2 = (LPCTSTR) pMyStruct->Message;

//        SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, s2);

    }

}

catch( int)

{

//MessageBox(hWnd, L"1", L"2", MB_OK);

}

return true;

}



Posted by 세모아
,


출처 : http://myblue0324.tistory.com/118


  1. // 방법1.  
  2. std::string str = "string";  
  3. std::wstring wstr = L"";  
  4.   
  5. wstr.assign(str.begin(), str.end());  
  6.   
  7. // 방법2.   
  8. USES_CONVERSION;  
  9.   
  10. std::string str = "string";  
  11. std::wstring wstr(A2W(str.c_str()));  


2. std::wstring을 std::string으로 변환.

  1. // 방법1.  
  2. std::wstring wstr = L"string";  
  3. std::string str = "";  
  4.   
  5. str.assign(wstr.begin(), wstr.end());  
  6.   
  7. // 방법2.  
  8. USES_UTF8_CONVERSION;  
  9.   
  10. std::wstring wstr = L"string";  
  11. str::string str(W2A(wstr.c_str()));  


위의 문자열을 CString형으로 변환하고자 할 경우에는 간단하게 아래와 같이 변경이 가능합니다.

  1. #ifdef _UNICODE  
  2.     std::wstring s = L"string";  
  3.     CString str(s.c_str());   
  4. #else  
  5.     std::string s = "string";  
  6.     CString str(s.c_str());   
  7. #endif  


Posted by 세모아
,

My 정리 :


std::string s2 = (LPSTR)pcds->lpData;


To "convert" a std::string to a LPCSTR depends on the exact context but usually calling .c_str() is sufficient.

This works.

void TakesString(LPCSTR param);

void f(const std::string& param)
{
    TakesString(param.c_str());
}

Note that you shouldn't attempt to do something like this.

LPCSTR GetString()
{
    std::string tmp("temporary");
    return tmp.c_str();
}

The buffer returned by .c_str() is owned by the std::string instance and will only be valid until the string is next modified or destroyed.



정의 -----------------------

LPSTR - (long) pointer to string - char *

LPCSTR - (long) pointer to constant string - const char *

LPWSTR - (long) pointer to Unicode (wide) string - wchar_t *

LPCWSTR - (long) pointer to constant Unicode (wide) string - const wchar_t *

LPTSTR - (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *

LPCTSTR - (long) pointer to constant TCHAR string - const TCHAR *



std:string 정의 페이지

http://www.cplusplus.com/reference/string/string/



원본 페이지 :



from: http://stackoverflow.com/questions/1200188/how-to-convert-stdstring-to-lpcstr


How can I convert a std::string to LPCSTR? Also, how can I convert a std::string to LPWSTR?

I am totally confused with these LPCSTR LPSTR LPWSTR LPCWSTR?

Are LPWSTR and LPCWSTR are the same?

shareimprove this question

str.c_str() gives you a const char *, which is an LPCSTR (Long Pointer to Constant STRing) -- means that it's a pointer to a 0 terminated string of characters. W means wide string (composed of wchar_t instead of char).

shareimprove this answer
4 
Minor picky point: on x64 LPCSTR would be a 64-bit pointer to a (constant) null-terminated string. –  Joel Jul 30 '09 at 14:41
1 
the C in LPCSTR stand for const not C –  mathk Jan 30 '13 at 9:14

Call c_str() to get a const char * (LPCSTR) from a std::string.

It's all in the name:

LPSTR - (long) pointer to string - char *

LPCSTR - (long) pointer to constant string - const char *

LPWSTR - (long) pointer to Unicode (wide) string - wchar_t *

LPCWSTR - (long) pointer to constant Unicode (wide) string - const wchar_t *

LPTSTR - (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *

LPCTSTR - (long) pointer to constant TCHAR string - const TCHAR *

You can ignore the L (long) part of the names -- it's a holdover from 16-bit Windows.

shareimprove this answer
3 
+1 Make it very easy. –  Sauron Jul 29 '09 at 13:24
   
Thank u very Much for ur support –  Cute Jul 29 '09 at 13:39

These are Microsoft defined typedefs which correspond to:

LPCSTR: pointer to null terminated const string of char

LPSTR: pointer to null terminated char string of char (often a buffer is passed and used as an 'output' param)

LPCWSTR: pointer to null terminated string of const wchar_t

LPWSTR: pointer to null terminated string of wchar_t (often a buffer is passed and used as an 'output' param)

To "convert" a std::string to a LPCSTR depends on the exact context but usually calling .c_str() is sufficient.

This works.

void TakesString(LPCSTR param);

void f(const std::string& param)
{
    TakesString(param.c_str());
}

Note that you shouldn't attempt to do something like this.

LPCSTR GetString()
{
    std::string tmp("temporary");
    return tmp.c_str();
}

The buffer returned by .c_str() is owned by the std::string instance and will only be valid until the string is next modified or destroyed.

To convert a std::string to a LPWSTR is more complicated. Wanting an LPWSTR implies that you need a modifiable buffer and you also need to be sure that you understand what character encodingthe std::string is using. If the std::string contains a string using the system default encoding (assuming windows, here), then you can find the length of the required wide character buffer and perform the transcoding using MultiByteToWideChar (a Win32 API function).

e.g.

void f(const std:string& instr)
{
    // Assumes std::string is encoded in the current Windows ANSI codepage
    int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);

    if (bufferlen == 0)
    {
        // Something went wrong. Perhaps, check GetLastError() and log.
        return;
    }

    // Allocate new LPWSTR - must deallocate it later
    LPWSTR widestr = new WCHAR[bufferlen + 1];

    ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);

    // Ensure wide string is null terminated
    widestr[bufferlen] = 0;

    // Do something with widestr

    delete[] widestr;
}
shareimprove this answer
   
How to Convert std::String to LPWSTR ??? –  Cute Jul 29 '09 at 13:44
   
Scholar and a gentleman! –  Russbear Aug 2 '11 at 22:02

Using LPWSTR you could change contents of string where it points to. Using LPCWSTR you couldn't change contents of string where it points to.

std::string s = SOME_STRING;
// get temporary LPSTR (not really safe)
LPSTR pst = &s[0];
// get temporary LPCSTR (pretty safe)
LPCSTR pcstr = s.c_str();
// convert to std::wstring
std::wstring ws; 
ws.assign( s.begin(), s.end() );
// get temporary LPWSTR (not really safe)
LPWSTR pwst = &ws[0];
// get temporary LPCWSTR (pretty safe)
LPCWSTR pcwstr = ws.c_str();

LPWSTR is just a pointer to original string. You shouldn't return it from function using the sample above. To get not temporary LPWSTR you should made a copy of original string on the heap. Check the sample below:

LPWSTR ConvertToLPWSTR( const std::string& s )
{
  LPWSTR ws = new wchar_t[s.size()+1]; // +1 for zero at the end
  copy( s.begin(), s.end(), ws );
  ws[s.size()] = 0; // zero at the end
  return ws;
}

void f()
{
  std::string s = SOME_STRING;
  LPWSTR ws = ConvertToLPWSTR( s );

  // some actions

  delete[] ws; // caller responsible for deletion
}
shareimprove this answer

The MultiByteToWideChar answer that Charles Bailey gave is the correct one. Because LPCWSTR is just a typedef for const WCHAR*widestr in the example code there can be used wherever a LPWSTR is expected or where a LPCWSTR is expected.

One minor tweak would be to use std::vector<WCHAR> instead of a manually managed array:

// using vector, buffer is deallocated when function ends
std::vector<WCHAR> widestr(bufferlen + 1);

::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), &widestr[0], bufferlen);

// Ensure wide string is null terminated
widestr[bufferlen] = 0;

// no need to delete; handled by vector

Also, if you need to work with wide strings to start with, you can use std::wstring instead of std::string. If you want to work with the Windows TCHAR type, you can use std::basic_string<TCHAR>. Converting from std::wstring to LPCWSTR or from std::basic_string<TCHAR> to LPCTSTR is just a matter of calling c_str. It's when you're changing between ANSI and UTF-16 characters that MultiByteToWideChar (and its inverse WideCharToMultiByte) comes into the picture.

shareimprove this answer

The conversion is simple:

std::string str; LPCSTR lpcstr = str.c_str();

shareimprove this answer

Converting is simple:

std::string myString;

LPCSTR lpMyString = myString.c_str();

One thing to be careful of here is that c_str does not return a copy of myString, but just a pointer to the character string that std::string wraps. If you want/need a copy you'll need to make one yourself using strcpy.

shareimprove this answer
   
How to Convert std::String to LPWSTR ??? –  Cute Jul 29 '09 at 13:44

The easiest way to convert a std::string to a LPWSTR is in my opinion:

  1. Convert the std::string to a std::vector<wchar_t>
  2. Take the address of the first wchar_t in the vector.

std::vector<wchar_t> has a templated ctor which will take two iterators, such as the std::string.begin() and .end() iterators. This will convert each char to a wchar_t, though. That's only valid if the std::string contains ASCII or Latin-1, due to the way Unicode values resemble Latin-1 values. If it contains CP1252 or characters from any other encoding, it's more complicated. You'll then need to convert the characters.


Posted by 세모아
,