c# - Read Barcode in an WP8.1 app with ZXing -
im writing first app wp8.1. app need scan barcode.
public async void scanbarcodeasync(windows.storage.storagefile afile) { writeablebitmap bitmap; bitmapdecoder decoder; using (irandomaccessstream str = await afile.openreadasync()) { decoder = await bitmapdecoder.createasync(str); bitmap = new writeablebitmap(convert.toint32(decoder.pixelwidth), convert.toint32(decoder.pixelheight)); await bitmap.setsourceasync(str); } zxing.barcodereader reader = new barcodereader(); /*reader.options.possibleformats = new zxing.barcodeformat[] { zxing.barcodeformat.code_128, zxing.barcodeformat.code_39 };*/ reader.options.tryharder = true; reader.autorotate = true; var results = reader.decode(bitmap); if (results != null) { edtbarcode.text = results.text; } else { edtbarcode.text = "error"; } }
the file method created way
async void startcapture() { var cameraid = await getcameraid(windows.devices.enumeration.panel.back); mediacaptureinitializationsettings settings = new mediacaptureinitializationsettings(); settings.photocapturesource = windows.media.capture.photocapturesource.videopreview; settings.streamingcapturemode = windows.media.capture.streamingcapturemode.video; settings.audiodeviceid = string.empty; settings.videodeviceid = cameraid.id; capturemanager = new mediacapture(); //define mediacapture object await capturemanager.initializeasync(settings); //initialize mediacapture , var focussettings = new focussettings(); focussettings.autofocusrange = autofocusrange.macro; focussettings.mode = focusmode.auto; focussettings.waitforfocus = true; focussettings.disabledriverfallback = false; capturemanager.videodevicecontroller.focuscontrol.configure(focussettings); capturemanager.setpreviewrotation(videorotation.clockwise90degrees); capturepreview.source = capturemanager; //start preiving on captureelement await capturemanager.startpreviewasync(); //start camera capturing } async private void capture_photo_click(object sender, routedeventargs e) { //create jpeg image encoding format storing image in jpeg type imageencodingproperties imgformat = imageencodingproperties.createjpeg(); // create storage file in local app storage if (imageindex > 0) { storagefile delfile = await applicationdata.current.localfolder.getfileasync("photo" + convert.tostring(imageindex - 1) + ".jpg"); await delfile.deleteasync(); } file = await applicationdata.current.localfolder.createfileasync("photo" + convert.tostring(imageindex++) + ".jpg", creationcollisionoption.replaceexisting); // take photo , store on file location. await capturemanager.capturephototostoragefileasync(imgformat, file); //// create storage file in picture library //storagefile file = await knownfolders.pictureslibrary.createfileasync("photo.jpg",creationcollisionoption.generateuniquename); // photo bitmapimage using storage file path. bmpimage = new bitmapimage(); bmpimage.createoptions = bitmapcreateoptions.none; bmpimage.urisource = new uri(file.path); imagetaken = true; frame.navigate(typeof(mainpage)); frame.navigate(typeof(mainpage)); }
i dont error running code. result zxing null. scanbarcodeasync-function directly run after taking picture. image app takes not sharp. can cause problem?
im happy suggestions solve problem.
Comments
Post a Comment