在捕获的图像上书写文本 [英] Writing Text on captured images

查看:68
本文介绍了在捕获的图像上书写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次开发应用程序开发人员,我正在尝试编写一个简单的应用程序,以便在从相机捕获后在图像上写入文本。我做了一些搜索,并为此做了很多资源。我已经写这样做以下。

I am a first time app developer and am trying to write a simple application to write text on images after capturing from camera. I did some searching and found a lot of resources for doing this this. I have written the following for doing this.

空隙cam_CaptureImageAvailable(对象发件人,Microsoft.Devices.ContentReadyEventArgs E)

&NBSP ;       {

            string fileName = savedCounter +" .jpg"; $


           试试
        {

            //解码JPEG流。


             Deployment.Current.Dispatcher.BeginInvoke(()=>


            &NBSP ; {

             //将照片加载到可写位图


             WriteableBitmap的WriteableBitmap的= PictureDecoder.DecodeJpeg(e.ImageStream);

 &NBSP ;            writeableBitmap.Invalidate();

      &NBSP ;      VAR renderText =新的TextBlock


                         {


      &NBSP ;                Text =" Hello Wo rld",


                           FontSize = 12,


                           Foreground =新的SolidColorBrush(Colors.White),


                           FontWeight = FontWeights.Black,


                          宽度= 500,


                          高度= 100


                           };




              writeableBitmap.Render(renderText,new TranslateTransform(){X = 100,Y = 300});


              writeableBitmap.Invalidate();




             使用(var ms = new MemoryStream())


              {

                  writeableBitmap.SaveJpeg(ms,1024,768,0,100);


                  ms.Seek(0,SeekOrigin.Begin);




                  library.SavePicture(fileName,ms);


              }


              e.ImageStream.Close();


              });




            Deployment.Current.Dispatcher.BeginInvoke(()=> {txtDebug.Text ="缩略图已保存到隔离存储。" ;;});


        }
           最后是
            {

                //关闭图像流

                e.ImageStream.Close();

            }
        }

void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
        {
            string fileName = savedCounter + ".jpg";

            try
        {
            // Decode the JPEG stream.
             Deployment.Current.Dispatcher.BeginInvoke(() =>
              {
              // load photo to writable bitmap
              WriteableBitmap writeableBitmap = PictureDecoder.DecodeJpeg(e.ImageStream);
              writeableBitmap.Invalidate();
              var renderText = new TextBlock
                           {
                           Text = "Hello World",
                           FontSize = 12,
                           Foreground = new SolidColorBrush(Colors.White),
                           FontWeight = FontWeights.Black,
                           Width = 500,
                           Height = 100
                           };

              writeableBitmap.Render(renderText, new TranslateTransform() {X = 100, Y = 300});
              writeableBitmap.Invalidate();

              using (var ms = new MemoryStream())
              {
                  writeableBitmap.SaveJpeg(ms, 1024, 768, 0, 100);
                  ms.Seek(0, SeekOrigin.Begin);

                  library.SavePicture(fileName, ms);
              }

              e.ImageStream.Close();
              });

            Deployment.Current.Dispatcher.BeginInvoke(() => {txtDebug.Text = "Thumbnail has been saved to isolated storage.";});
        }
            finally
            {
                // Close image stream
                e.ImageStream.Close();
            }
        }

但它投掷FileNotFoundException异常而创建具有以下堆栈跟踪的对象WritableBitMap:

But it is throwing the FileNotFoundException while creating the WritableBitMap object with the following stack trace:

{System.IO.FileNotFoundException:未能找到文件C:\Data\Programs\ {243782DF-609A-4044-AD0C-CB741E467C58} \Install\tempFilef74071cc-4c7e-45eb-a0b2-f888053a301e.jpg'

档。名称: 'C:\Data\Programs\ {243782DF-609A-4044-AD0C-CB741E467C58} \Install\tempFilef74071cc-4c7e-45eb-a0b2-f888053a301e.jpg'

&NBSP ; 在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)

   at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolean useLongPath,
Boolean checkHost)

  在System.IO.FileStream..ctor(字符串路径,的FileMode模式,FileAccess的访问,文件共享份额,缓冲区大小的Int32,FileOptions选项,字符串MSGPATH,布尔bFromProxy,布尔useLongPath,布尔checkHost)

   在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享)

  在Microsoft.Devices.PhotoStream..ctor(String localFileName)

  在Microsoft.Devices.StreamHelper.CreateCachedStream(Stream s)

  在Microsoft.Devices.StreamHelper..ctor(Stream s)

  在Microsoft.Phone.PictureDecoder.DecodeJpegInternal(流源,Int32 maxPixelWidth,Int32 maxPixelHeight)

  在Microsoft.Phone.PictureDecoder.DecodeJpeg(流源)

  在sdkBasicCameraCS.MainPage<> c__DisplayClassd< cam_CaptureImageAvailable> b__a()}

{System.IO.FileNotFoundException: Could not find file 'C:\Data\Programs\{243782DF-609A-4044-AD0C-CB741E467C58}\Install\tempFilef74071cc-4c7e-45eb-a0b2-f888053a301e.jpg'.
File name: 'C:\Data\Programs\{243782DF-609A-4044-AD0C-CB741E467C58}\Install\tempFilef74071cc-4c7e-45eb-a0b2-f888053a301e.jpg'
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Microsoft.Devices.PhotoStream..ctor(String localFileName)
   at Microsoft.Devices.StreamHelper.CreateCachedStream(Stream s)
   at Microsoft.Devices.StreamHelper..ctor(Stream s)
   at Microsoft.Phone.PictureDecoder.DecodeJpegInternal(Stream source, Int32 maxPixelWidth, Int32 maxPixelHeight)
   at Microsoft.Phone.PictureDecoder.DecodeJpeg(Stream source)
   at sdkBasicCameraCS.MainPage.<>c__DisplayClassd.<cam_CaptureImageAvailable>b__a()}

有人可以让我知道我做错了,或者如果我需要修改任何开发设置。

Can someone let me know what I am doing wrong here or if I need to modify any development settings.

谢谢,

Suhail

推荐答案

首先创建一个Bitmap对象然后再写下WritableBitmap来解决问题:

the issue got solved by first creating a Bitmap object and then the WritableBitmap as follows:

试试
  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

                e.ImageStream.Seek(0,SeekOrigin.Begin);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; MemoryStream ms1 = new MemoryStream();



              &NBSP;&NBSP; e.ImageStream.CopyTo(MS1);&NBSP;&NBSP;  

                //将信息写入UI线程。

                Deployment.Current.Dispatcher.BeginInvoke(委托()&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; txtDebug.Text = QUOT;捕获可用的图像,保存图像英寸;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的BitmapImage b =新的BitmapImage();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; b.CreateOptions = BitmapCreateOptions.None;

               &n BSP;&NBSP;&NBSP;&NBSP; //e.ImageStream

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; b.SetSource(MS1);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //PictureImage.Source
= b;





        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; WriteableBitmap writeableBitmap = new WriteableBitmap(b);

try
            {
                e.ImageStream.Seek(0, SeekOrigin.Begin);
                MemoryStream ms1 = new MemoryStream();

                e.ImageStream.CopyTo(ms1);    
                // Write message to the UI thread.
                Deployment.Current.Dispatcher.BeginInvoke(delegate()                
                {
                    txtDebug.Text = "Captured image available, saving picture.";
                    
                    BitmapImage b = new BitmapImage();
                    b.CreateOptions = BitmapCreateOptions.None;
                    //e.ImageStream
                    b.SetSource(ms1);
                                        //PictureImage.Source = b;


                    WriteableBitmap writeableBitmap = new WriteableBitmap(b);

谢谢。


这篇关于在捕获的图像上书写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆