海思3516 zbar识别二维码,并保存yuv图像代码

/******************************************************************************
  Some simple Hisilicon Hi35xx video input functions.

  Copyright (C), 2010-2011, Hisilicon Tech. Co., Ltd.
 ******************************************************************************
    Modification:  2011-2 Created
******************************************************************************/
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "hi_comm_video.h"
#include "mpi_vi.h"
#include "zbar.h"
//#include "test_images.h"


#include "sample_comm.h"
#define MAX_FRM_WIDTH   2048
static HI_CHAR* pUserPageAddr[2] = {HI_NULL,HI_NULL};
static HI_U32 u32Size = 0;

static FILE* pfd = HI_NULL;





HI_S32 SAMPLE_COMM_VPSS_GetImg(VPSS_GRP VpssGrp, VPSS_CHN VpssChn)
{				
				printf("grp:%d,chn:%d\n",VpssGrp,VpssChn);
				zbar_image_scanner_t *scanner = NULL;
					/* create a reader */
				scanner = zbar_image_scanner_create();
				
					/* configure the reader */
				zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
				
					 /* obtain image data */
				int width=0,height=0;
                HI_S32 s32ret;
				 HI_VOID* yPtr;
                VIDEO_FRAME_INFO_S stFrame;
                HI_U32 u32Depth,i;
				char * pVBufVirt_Y;
				HI_CHAR *pUserPageAddr[2];
				HI_U32 u32UvHeight;
				 char* pVBufVirt_C;
				 HI_CHAR szYuvName[128];
				  char* pMemContent;
				  HI_CHAR szPixFrm[10];
				  unsigned int w, h;
				  static unsigned char TmpBuff[MAX_FRM_WIDTH]; 
				  HI_U32 u32Cnt = 1;
				  snprintf(szPixFrm, 10, "P420");
                /* set max depth */
                u32Depth =3;
				 HI_U32 len;
				 snprintf(szYuvName, 128, "./vpss_grp%d_chn%d_w%d_h%d_%s_%d.yuv", VpssGrp, VpssChn,
            stFrame.stVFrame.u32Width, stFrame.stVFrame.u32Height, szPixFrm, u32Cnt);
   			 printf("Dump YUV frame of vpss chn %d  to file: \"%s\"\n", VpssChn, szYuvName);
   			 fflush(stdout);
			  pfd = fopen(szYuvName, "wb");
                s32ret = HI_MPI_VPSS_SetDepth(VpssGrp,VpssChn,u32Depth);//HI_MPI_VPSS_SetDepth
                if (HI_SUCCESS != s32ret)
                {
                                printf("set max depth err:0x%x\n", s32ret);
                                return s32ret;
                }
               
                   while(1)
                   	{
                		
                        /* get video frame from vpss chn */
                        s32ret = HI_MPI_VPSS_GetChnFrame(VpssGrp,VpssChn,&stFrame,-1); 
                        if (HI_SUCCESS != s32ret)
                        {         printf("get vpss frame err:0x%x\n", s32ret); 
                                  return s32ret;
                        }
						width=stFrame.stVFrame.u32Width;
						height=stFrame.stVFrame.u32Height;
						//raw=(void*)malloc(2*width*height);
						zbar_image_t *image = zbar_image_create();
						zbar_image_set_format(image, *(int*)"NV21");
						zbar_image_set_size(image, width, height);
                        printf("get vpss frame u32Width: 0x%x\n", stFrame.stVFrame.u32Width);
                        printf("get vpss frame u32Height: 0x%x\n", stFrame.stVFrame.u32Height);
						u32UvHeight = stFrame.stVFrame.u32Height/2;
						//len = (stFrame.stVFrame.u32Stride[0])*(stFrame.stVFrame.u32Height)+(stFrame.stVFrame.u32Stride[1])*u32UvHeight;
						len = (stFrame.stVFrame.u32Stride[0])*(stFrame.stVFrame.u32Height)*3 / 2;
    				//	yPtr= HI_MPI_SYS_Mmap(stFrame.stVFrame.u32PhyAddr[0], len);
						 pUserPageAddr[0] = (HI_CHAR *) HI_MPI_SYS_Mmap(stFrame.stVFrame.u32PhyAddr[0], len);
						 pVBufVirt_Y = pUserPageAddr[0]; 
						 pVBufVirt_C = pVBufVirt_Y + (stFrame.stVFrame.u32Stride[0]) * (stFrame.stVFrame.u32Height);

						 fprintf(stderr, "saving......Y......");
					   fflush(stderr);
					   for (h = 0; h < stFrame.stVFrame.u32Height; h++)
					   {
						   pMemContent = pVBufVirt_Y + h * stFrame.stVFrame.u32Stride[0];
						   fwrite(pMemContent, stFrame.stVFrame.u32Width, 1, pfd);
					   }

					    fflush(pfd);
        /* save U ----------------------------------------------------------------*/
        fprintf(stderr, "U......");
        fflush(stderr);
    
        for (h = 0; h < u32UvHeight; h++)
        {
            pMemContent = pVBufVirt_C + h * stFrame.stVFrame.u32Stride[1];

            pMemContent += 1;

            for (w = 0; w < stFrame.stVFrame.u32Height / 2; w++)
            {
                TmpBuff[w] = *pMemContent;
                pMemContent += 2;
            }
            fwrite(TmpBuff, stFrame.stVFrame.u32Width / 2, 1, pfd);
        }
        fflush(pfd);

        /* save V ----------------------------------------------------------------*/
        fprintf(stderr, "V......");
        fflush(stderr);
        for (h = 0; h < u32UvHeight; h++)
        {
            pMemContent = pVBufVirt_C + h * stFrame.stVFrame.u32Stride[1];

            for (w = 0; w < stFrame.stVFrame.u32Width / 2; w++)
            {
                TmpBuff[w] = *pMemContent;
                pMemContent += 2;
            }
            fwrite(TmpBuff, stFrame.stVFrame.u32Width / 2, 1, pfd);
        }
    
    fflush(pfd);

    fprintf(stderr, "done %d, 0x%x!\n", stFrame.stVFrame.u32TimeRef, stFrame.stVFrame.u32PhyAddr[0]);
    fflush(stderr);
	
                     zbar_image_set_data(image,pVBufVirt_Y, (stFrame.stVFrame.u32Stride[0])*(stFrame.stVFrame.u32Height), NULL);
					 
					 zbar_image_t *test=zbar_image_convert(image,*(int*)"Y800");
					 s32ret= HI_MPI_VPSS_ReleaseChnFrame(VpssGrp,VpssChn,&stFrame);
						
                        if (HI_SUCCESS != s32ret)
                        {                printf("Release vi frame err:0x%x\n", s32ret); 
                                        return s32ret;
                                    
                        }
					  
                      /* scan the image for barcodes */
					  
        			int n = zbar_scan_image(scanner, test);//二维码扫描
        			printf("n=%d\n",n);
					  if(n==0)
			            continue;
			        /* extract results */
			        const zbar_symbol_t *symbol = zbar_image_first_symbol(test);
			        for(; symbol; symbol = zbar_symbol_next(symbol)) 
			        {	 
			            /* do something useful with results */
			            zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
			            const char *data = zbar_symbol_get_data(symbol);
						 
			            printf("decoded %s symbol \"%s\"\n",
			                   zbar_get_symbol_name(typ), data);
			        }


					
					
					   
					HI_MPI_SYS_Munmap(pUserPageAddr[0], len);
						 

                    

						
                   	}
                return s32ret;
				
}




你可能感兴趣的:(海思3516 zbar识别二维码,并保存yuv图像代码)