c语言 实现bmp转pdf 问题

lanting918 2016-10-10 12:53:05
在网上找了份jpeg2pdf的代码, 尝试修改成bmp2pdf, 不过失败了
不知道有没有大侠做过这个 指点一下, 目前改法是直接将jpeg图片换成bmp 然后把需要的宽度高度等信息直接用bmp的替换
...全文
321 点赞 收藏 6
写回复
6 条回复
lanting918 2016年10月12日
引用 2 楼 u010808402 的回复:
代码没细看,不过Jpeg和Bmp对图片像素点的编码方式并不相同,简单替换buffer应该会有问题。 你可以试着通过mspaint或者其他工具将bmp先转换成jpeg然后测试下本段代码是否可行。 如可行,则只需做BMP移植,如不可行,则需换别的代码。
引用 3 楼 zhao4zhong1 的回复:
使用GDI+将bmp转jpg再调用jpeg2pdf
有嵌入式适合的bmp转jpeg算法吗? 这块我同事写了一个 转一副240*200的bmp 到jpeg要花1s多时间 受不了
回复 点赞
lanting918 2016年10月12日
将BMP转jpg的方式是可行的 , 本段代码对jpeg 没问题。 不过我们的设备里面做转换的时候效率不高 我再试试 谢谢
回复 点赞
赵4老师 2016年10月12日
嵌入式有GDI+吗?
回复 点赞
赵4老师 2016年10月10日
使用GDI+将bmp转jpg再调用jpeg2pdf
回复 点赞
Sniper_Pan 2016年10月10日
代码没细看,不过Jpeg和Bmp对图片像素点的编码方式并不相同,简单替换buffer应该会有问题。 你可以试着通过mspaint或者其他工具将bmp先转换成jpeg然后测试下本段代码是否可行。 如可行,则只需做BMP移植,如不可行,则需换别的代码。
回复 点赞
lanting918 2016年10月10日
STATUS Jpeg2PDF_AddJpeg(PJPEG2PDF pPDF, UINT32 imgW, UINT32 imgH, UINT32 fileSize, UINT8 *pJpeg, UINT8 isColor, PageOrientation pageOrientation, double dpiX, double dpiY, ScaleMethod scale, bool cropHeight, bool cropWidth) { STATUS result = ERROR; PJPEG2PDF_NODE pNode; double imgAspect, newImgW, newImgH; if(pPDF) { if(pPDF->nodeCount >= MAX_PDF_PAGES) { logMsg("Add JPEG into PDF Skipped. Reason: Reached Max Page Number (%d) in single PDF.\n", MAX_PDF_PAGES,2,3,4,5,6); return result; } pNode = (PJPEG2PDF_NODE)malloc(sizeof(JPEG2PDF_NODE)); if(pNode) { UINT32 nChars, currentImageObject; UINT8 *pFormat, lenStr[256]; pNode->JpegW = imgW; pNode->JpegH = imgH; pNode->JpegSize = fileSize; pNode->pJpeg = (UINT8 *)malloc(pNode->JpegSize); pNode->pNext = NULL; if(pNode->pJpeg != NULL) { bool jpegPortrait, pagePortrait; double pageWidth, pageHeight; // accounting for page orientation, in PDF units (pixels at PDF_DOT_PER_INCH dpi) double maxImgWidth, maxImgHeight; // actual values accounting for page orientation, in PDF units double jpegWidth, jpegHeight; // jpeg dimensions (accounting for dpiX, dpiY, PDF_DOT_PER_INCH), in PDF units Fit fit; memcpy(pNode->pJpeg, pJpeg, pNode->JpegSize); /* Image Object */ Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n'); currentImageObject = pPDF->pdfObj; pPDF->currentOffSet += sprintf(pNode->preFormat, "\n%d 0 obj\n<</Subtype/Image/Length 670/Width 248/Height 200/BitsPerComponent 1/ColorSpace/DeviceGray/Filter/JBIG2Decode/Interpolate true/Decode[ 1 0]>>stream\n", pPDF->pdfObj, ((isColor)? "DeviceRGB" : "DeviceGray"), pNode->JpegW, pNode->JpegH, pNode->JpegSize); pPDF->currentOffSet += pNode->JpegSize; pFormat = pNode->pstFormat; nChars = sprintf(pFormat, "\nendstream\nendobj\n"); pPDF->currentOffSet += nChars; pFormat += nChars; pPDF->pdfObj++; /* determine scale of the image keeping aspect ratio */ jpegWidth=((double)pNode->JpegW)*PDF_DOT_PER_INCH/dpiX; jpegHeight=((double)pNode->JpegH)*PDF_DOT_PER_INCH/dpiY; imgAspect=jpegWidth/jpegHeight; if(JPEG2PDF_DEBUG) logMsg("imgAspect = %f %d %d\n", imgAspect, pNode->JpegW, pNode->JpegH, 3,4,5,6); // Determine page orientation: jpegPortrait=(jpegWidth <= jpegHeight); if(pageOrientation==PageOrientationAuto){ if(scale==ScaleNone && jpegWidth<=pPDF->maxImgW && jpegHeight<=pPDF->maxImgH){ // image already fits into available area, don't rotate the page pagePortrait=true; // assuming portrait orientation the most convenient for most users }else{ pagePortrait=jpegPortrait; if((pPDF->maxImgW < pPDF->maxImgH) ^ (pPDF->pageW < pPDF->pageH)){ // very rare case: page orientation is opposite to available area orientation (it's possible with differently sized margins) pagePortrait=!pagePortrait; } } }else{ pagePortrait=(pageOrientation==Portrait) ? true : false; } maxImgWidth=pagePortrait ? pPDF->maxImgW : pPDF->maxImgH; maxImgHeight=pagePortrait ? pPDF->maxImgH : pPDF->maxImgW; // Determine scaling method: if(scale==ScaleFit || (scale==ScaleReduce && (jpegWidth>maxImgWidth || jpegHeight>maxImgHeight))){ // fit jpeg to available area if(maxImgWidth/maxImgHeight > imgAspect){ // available area aspect is wider than jpeg aspect fit=FitHeight; }else{ // jpeg aspect is wider than available area aspect fit=FitWidth; } }else if(scale==ScaleFitWidth || (scale==ScaleReduceWidth && jpegWidth>maxImgWidth)){ fit=FitWidth; }else if(scale==ScaleFitHeight || (scale==ScaleReduceHeight && jpegHeight>maxImgHeight)){ fit=FitHeight; }else{ // don't fit, keep original dpi fit=FitNone; } // Scale image: if(fit==FitWidth){ newImgW=maxImgWidth; newImgH=maxImgWidth/imgAspect; }else if(fit==FitHeight){ newImgW=maxImgHeight*imgAspect; newImgH=maxImgHeight; }else{//fit==FitNone newImgW=jpegWidth; newImgH=jpegHeight; } // Set paper size from image size (possibly fitted/reduced to specific paper size) or properly rotate the page: pageWidth=cropWidth ? (newImgW+pPDF->margin) : (pagePortrait ? pPDF->pageW : pPDF->pageH); pageHeight=cropHeight ? (newImgH+pPDF->margin) : (pagePortrait ? pPDF->pageH : pPDF->pageW); /* Page Object */ Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n'); pNode->PageObj = pPDF->pdfObj; nChars = sprintf(pFormat, "%d 0 obj\n<</Type/Page/Parent 1 0 R/MediaBox[0 0 %.2f %.2f]/Contents %d 0 R/Resources %d 0 R>>\nendobj\n", pPDF->pdfObj, pageWidth, pageHeight, pPDF->pdfObj+1, pPDF->pdfObj + 3); pPDF->currentOffSet += nChars; pFormat += nChars; pPDF->pdfObj++; /* Contents Object in Page Object */ Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n'); sprintf(lenStr, "q\n1 0 0 1 %.2f %.2f cm\n%.2f 0 0 %.2f 0 0 cm\n/I%d Do\nQ\n", (pageWidth-newImgW)/2, (pageHeight-newImgH)/2, newImgW , newImgH , pPDF->imgObj); // center image nChars = sprintf(pFormat, "%d 0 obj\n<</Length %d 0 R>>stream\n%sendstream\nendobj\n", pPDF->pdfObj, pPDF->pdfObj+1, lenStr); pPDF->currentOffSet += nChars; pFormat += nChars; pPDF->pdfObj++; /* Length Object in Contents Object */ Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n'); nChars = sprintf(pFormat, "%d 0 obj\n%ld\nendobj\n", pPDF->pdfObj, strlen(lenStr)); pPDF->currentOffSet += nChars; pFormat += nChars; pPDF->pdfObj++; /* Resources Object in Page Object */ Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n'); nChars = sprintf(pFormat, "%d 0 obj\n<</ProcSet[/PDF/%s]/XObject<</I%d %d 0 R>>>>\nendobj\n", pPDF->pdfObj, ((isColor)? "ImageC" : "ImageB"), pPDF->imgObj, currentImageObject); pPDF->currentOffSet += nChars; pFormat += nChars; pPDF->pdfObj++; pPDF->imgObj++; /* Update the Link List */ pPDF->nodeCount++; if(1 == pPDF->nodeCount) { pPDF->pFirstNode = pNode; } else { pPDF->pLastNode->pNext = pNode; } pPDF->pLastNode = pNode; result = OK; } /* pNode->pJpeg allocated OK */ } /* pNode is valid */ } /* pPDF is valid */ return result; } 目前直接把这个函数的jpeg buf用bmp buf替换 生成的pdf看不到图
回复 点赞
发动态
发帖子
C语言
创建于2007-09-28

3.2w+

社区成员

24.0w+

社区内容

C语言相关问题讨论
社区公告
暂无公告