WebJob to read from and write to the same Azure blob - azure-blob-storage

I am trying to process images uploaded to azure using webjob. I have 2 containers image and thumbs.
Currently, I am reading from image container, creating a thumbnail and writing it to thumbs container using the following code, which works great.
public static void GenerateThumbnail([QueueTrigger("addthumb")] ImageDTO blobInfo,
[Blob("images/{Name}", FileAccess.Read)] Stream input, [Blob("thumbs/{Name}")] CloudBlockBlob outputBlob)
{
using (Stream output = outputBlob.OpenWrite())
{
ConvertImageToThumbnail(input, output, blobInfo.Name);
outputBlob.Properties.ContentType = GetMimeType(blobInfo.Name);
}
}
Now, I would also like to resize the main image from image container (if it's too big), compress it and replace the original with it.
Is there a way to read from and write to the same blob?

Yes, you can read/write to the same blob. For example you could change your input binding to bind to CloudBlockBlob using FileAccess.ReadWrite:
public static void GenerateThumbnail(
[QueueTrigger("addthumb")] ImageDTO blobInfo,
[Blob("images/{Name}", FileAccess.ReadWrite)] CloudBlockBlob input,
[Blob("thumbs/{Name}")] CloudBlockBlob output)
{
// Process the image
}
You can then access the OpenRead/OpenWrite stream methods on that blob to read the image blob and process/modify it as needed.

Related

How to Transform a Blob to An Image in flutter

i want to display an image stored in mysql database
the problem is that i can't convert the blob format into a Uint8list ; i searched and found soulitions but none of them works
Grab the blob from JSON:
var blob = yourJSONMapHere['yourJSONKeyHere'];
var image = BASE64.decode(blob); // image is a Uint8List
Now, use image in a Image.memory
new Container( child: new Image.memory(image));
this soulition didn't work because base64.decode need a string source not a blob file to convert
Don't know if it is relatable to this particular case but had similar issue with Cloud Firestore.
Created blob for storing in this way:
Blob myBlob = Blob(await audioFile.readAsBytes());
saved to Firestore in one field as usual
Then tried to read it back and couldn't figure it out how to get Uint8List from blob I get back from Firestore.
my solution:
//extract blob from field of Firestore document
Blob audioBlob = audioDocFromDb.get("fieldName");
//use .bytes on blob from this source
//package:cloud_firestore_platform_interface/src/blob.dart
Uint8List audioBytes = audioBlob.bytes;
this worked for me.
In my case I was packing up recorded audio and trying to play it back.
I had this problem too, i know the solution now, after many attempts:
Dont forget to upvote!
Uint8List image = Uint8List.fromList(blob.toBytes());
Image.memory(image);
This function has always saved me for getting bytes from a file that is uploaded to a URL.
import 'package:http/http.dart' as http;
// urlImageBlob is the URL where our file is hosted.
Uint8List fileBytes = await http.readBytes(Uri.parse(urlImageBlob));
// Display if are image.
Image.memory(fileBytes);

Xamarin [RestSharp] + [Xam.Plugin.Media] upload model containing image

I'm trying to upload an image from xamarin.forms and using restsharp for api service.
RestRequest uploadPostRestRequest = new RestRequest("post/create/", Method.POST);
uploadPostRestRequest.AddJsonBody(uploadPostRequest);
and this is my model UploadPostRequest
public class UploadPostRequest
{
public string content;
public byte[] image;
}
Question - Is it right to set image type as byte[]?
Would server accept this or would restsharp manage it?
If RestSharp has a nice control with this, can I just put MediaFile from Xam.Plugin.Media so I can upload it right over?
Xam.Plugin.Media is used for picking images from mobile device.
Too many options, so, that's why I'm looking for good advice.
Has anyone experienced this same issue before? please help.
For additional info, I cant use System.IO.File, Xamarin.Forms wont let me use it.
When the Xam.Plugin.Media finish loading the media either from the Camera or from the Library it returns a MediaFile. This object can be converter to a byte array with something like this:
byte[] byteArray;
using (var memoryStream = new MemoryStream ())
{
mediaFile.GetStream ().CopyTo (memoryStream);
mediaFile.Dispose ();
byteArray = memoryStream.ToArray ();
}
Now you have the byte array you just need to pass it to the method that will upload the image to your backend.

How to extract text from image Android app

I am working on a feature for my Android app. I would like to read text from a picture then save that text in a database. Is using OCR the best way? Is there another way? Google suggests in its documentation that NDK should only be used if strictly necessary but what are the downfalls exactly?
Any help would be great.
you can use google vision library for convert image to text, it will give better output from image.
Add below library in build gradle:
compile 'com.google.android.gms:play-services-vision:10.0.0+'
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
Frame imageFrame = new Frame.Builder()
.setBitmap(bitmap) // your image bitmap
.build();
String imageText = "";
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
imageText = textBlock.getValue(); // return string
}
From this Simple example of OCRReader in Android tutorial you can read text from image and also you can scan for text using camera, using very simple code.
This library is developed using Mobile Vision Text API
For scan text from camera
OCRCapture.Builder(this)
.setUseFlash(true)
.setAutoFocus(true)
.buildWithRequestCode(CAMERA_SCAN_TEXT);
For extract text from image
String text = OCRCapture.Builder(this).getTextFromUri(pickedImage);
//You can also use getTextFromBitmap(Bitmap bitmap) or getTextFromImage(String imagePath) buplic APIs from OCRLibrary library.
Text from an image can be extracted using Firebase machine learning (ML) kit. There are two versions of the text recognition API, on-device API (free) and on-cloud API.
To use the API, first create BitMap of the image, which should be upright. Then create FirebaseVisionImage object passing the bitmap object.
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Then create FirebaseVisionTextRecognizer object.
FirebaseVisionTextRecognizer textRecognizer = FirebaseVision.getInstance()
.getCloudTextRecognizer();
Then pass the FirebaseVisionImage object to processImage() method, add listeners to the resulting task and capture the extracted text in success callback method.
textRecognizer.processImage(image)
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
#Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
//process success
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
//process failure
}
});
For complete example which shows how to use Firebase ML text recognizer, see https://www.zoftino.com/extracting-text-from-images-android
There is a different option. You can upload your image to the server, OCR it from the server, then get the result.

Custom VideoSink and CaptureSource frame encoding

I'm using CaptureSource to get video stream. In order to save video I can use outof the box FileSink which allows me to save mp4 encoded file but in my application I want to add some adjustments to video stream (add some artifacts, text, logo, etc) and then save it to isolated storage.
I could define derived from VideoSink class and override OnSample method:
protected override void OnSample(long sampleTimeInHundredNanoseconds, long frameDurationInHundredNanoseconds, byte[] sampleData)
{
//process sampleData
//encode sampleData
//save encoded sampleData
}
But I'm not sure how to encode raw video data on Windows Phone.
I'm looking for any video encoders for WP.
TIA for advice!

Create image serving URL for new Blob file

After using the ImagesService to transform an uploaded image, I would like to store it back into a new Blob file and make it available through getServingUrl() as provided by the ImagesService.
Storing the image in a new AppEngineFile as described here works fine and I am able to open and view it locally using the dev server.
However when passing the blobKey for the new AppEngineFile to ImagesService.getServingUrl() a
java.lang.IllegalArgumentException: Could not read blob.
exception is thrown. Any ideas what the problem could be? This is the code I use to transform and store an uploaded image (blobKey and blobInfo correspond to the uploaded file, not the newly created one).
/* Transform image in existing Blob file */
Image originalImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform verticalFlip = ImagesServiceFactory.makeVerticalFlip();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image newImage = imagesService.applyTransform(verticalFlip, originalImage);
/* Store newImage in an AppEngineFile */
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(blobInfo.getContentType());
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
ByteBuffer buffer = ByteBuffer.wrap(newImage.getImageData());
writeChannel.write(buffer);
/* closeFinally assigns BlobKey to new file object */
writeChannel.closeFinally();
BlobKey newBlobKey = fileService.getBlobKey(file);
Edit:
The above code is correct, the problem was storing a String representation of the new blob key using newBlobKey.toString() instead of newBlobKey.getKeyString().
Why would you want to do that? Once you transform an image it is cached and anyway it is always fast. If you really feel you want to save it just use urlfetch to read the data and store them in the BlobStore ;-)
The following works fine when executed at the end of the code posted in the question:
String url = imagesService.getServingUrl(newBlobKey)
The URL can then be used to scale and crop the new image as described in the docs
http://code.google.com/appengine/docs/java/images/overview.html#Transforming_Images_from_the_Blobstore

Resources