SolrJ how to get data from binary field - solr

I have data (small picture) in binary format in Solr 6, when using SolrJ library I got in the response reference like this [B#157dac6d but not the base64 string from Solr. Any idea how to extract the data back?
Example png:
"preview_db":"iVBORw0KGgoAAAA...ABJRU5ErkJggg"

SolrJ returns the solr.BinaryField as byte[] array (class [B).
So to get the binary as a base64 String again we need to cast the response value, encode the byte array and then create String.
byte[] bytes = (byte[]) filedValue;
byte[] encoded = Base64.encodeBase64(bytes);
String s = new String(endcoded);
System.out.println(s);

Related

Convert Mat to byte[] - .net core

I'm trying to use EmgCV with .netCore and I want to post my image to an server.
How can I convert my Image (Mat or Image<Bgr, byte>) to byte[].
I cant convert it to Bitmap first, since im using .netcore and not framework. That's the way how I convert my byte[] to a Mat:
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
Mat image2 = new Mat(4000, 6000, DepthType.Cv8U, 3);
CvInvoke.Imdecode(bytes, ImreadModes.AnyColor, image2);
Can someone help me to do it the other way round? I tried using this, but this doesn't work:
Image<Bgr, byte> afterImage = diff.ToImage<Bgr, byte>();
byte[] afterbytes = afterImage.Bytes;
Thanks in advance!
I think that the bytes you receive are not raw bitmap bytes. Instead you receive a jpeg or png encoded image. Thats why you have to use Imdecode.
EmguCV also offers method Imencode to encode images into various formats.
Example below encodes image2 Mat as jpeg bytes. Can also be png or something else when supported by EmguCV.
var buffer = new VectorOfByte();
CvInvoke.Imencode(".jpg", image2, buffer); //Must use .jpg not jpg
byte[] jpgBytes = buffer.ToArray();
Instead of Mat you can also pass Image<TColor,TDepth>
I found a solution by myself:
Image<Bgr, byte> afterImage = afterImageMat.ToImage<Bgr, byte>();
byte[] afterbytes = afterImage.ToJpegData();

Detect file type and decode the file from Base64 format in AngularJs

How to find the file type whether it is an image or ppt or pdf from Base64 string.
How can i decode that string and also get the file extension of Base64 String.
It should be in AngularJs.
If the string was obtained by FileReader.readAsDataURL() (see https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL) it should be similar to this one:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQg ... "
So the first part is the MIME_TYPE (data:image/jpeg;), then there's a delimitator (base64,) and finally the base64 string which can be decoded using a library like this one:
https://github.com/ninjatronic/angular-base64

Convert bitmap to byte[]

I am developing a LightSwitch application that generates barcodes (QR images) for tickets. I am calling an encode function that converts text to bitmap.
I just need to save this in an LightSwitch Image field.
I have this:
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
EditableImage image = qrCodeEncoder.Encode(data);
I want this:
ticket.QRImage = .....???
I am using this library for the QR
http://www.jeff.wilcox.name/2009/09/quick-read-silverlight-barcodes/
http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library
You can get the bytes by calling image.GetStream(), and then using one of the standard methods to get the bytes out of the stream (see How to convert an Stream into a byte[] in C#?)

MD5 of file downloaded from database, from a JSONObject

My requirement is to compare the MD5 hashes of a file on the local disk and a file downloaded from a database.
The file is stored on SQL Server in a VARBINARY(MAX) column. The file can be any type. I'm currently testing with a PDF file. I get the file from the database using a HttpPost request. A JSONObject is built using the HttpResponse object. The JSONObject contains the file contents in binary format.
Now I have to compare the MD5 hash of the received binary data against the MD5 hash of the same file on disk. I have written the following code but the MD5 hashes do not match.
I think I'm going wrong in simply calculating the MD5 of the downloaded binary contents. Is there a correct way to do this? Thanks in advance.
// Read response from a HttpResponse object 'response'
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line="";
StringBuilder sb = new StringBuilder();
while((line=reader.readLine())!=null) {
sb.append(line);
}
// construct jsonobject
JSONObject jsonResponse = new JSONObject(sb.toString());
//Read file from disk
FileInputStream fis = new FileInputStream(new File(this.getClass().getResource("C:\\demo.pdf").getPath()));
// Calculate MD5 of file read from disk
String md5Request = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
// Calculate MD5 of binary contents. "binfile" is name of key in the JSONObject
// and binary contents of downloaded file are in its corresponding value field
String md5Response = org.apache.commons.codec.digest.DigestUtils.md5Hex(jsonResponse.getString("binfile"));
Assert.assertEquals("Hash sums of request and response must match", md5Request, md5Response);
When I debug, I see this value against the binfile key in the JSONObject 'jsonResponse'
binfile=[37,80,68,70,45,49,46,52,13,37,-30,-29,-49,-45,13,10,52,48...]
and what follows is a lengthy stream of binary data.
OK, in SQL there's a build-in function that looks like this:
select *,
convert(varchar(50),master.sys.fn_repl_hash_binary(a.BinaryField),2) as 'MD5Hash'
from SomeTable a
You give the fn_repl_hash_binary the name of the binary field you're reading, plus "2" as an argument which tells SQL to calc the value as an MD5; I think "1" is SHA.
And in Java, you can use something like this:
private String getMD5Hash(byte[] bytes) throws java.lang.Exception{
String s="This is a test";
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(bytes,0,bytes.length);
return new BigInteger(1,m.digest()).toString(16);
}
This should do the trick. Best of luck, CodeWarrior.
It is not a new post but here is a possible solution, as I faced this problem too on python and made a bunch of test to find how to do...
As you treat all data in binary, you need to open the file to compare in binary mode.
My original code that was failing every time to read the correct MD5 checksum:
with open(filepath, "r") as file_to_check:
tile_file = file_to_check.read()
Corrected code:
with open(filepath, "rb") as file_to_check:
tile_file = file_to_check.read()
Simply adding the b (binary) after the read (r) flag to let python know it need to read the file as binary and now it works.
This might be what will help you find your problem... Hope it helps!

Java - byte[] to FileItem

I'm using JasperReports to generate PDFs, and it gives me the PDF as a byte array, byte[].
I want to pass the raw bytes to another function that needs the file in terms of a FileItem object. In particular, the FileItem is from the Apache Commons library org.apache.commons.fileupload.FileItem.
// the function I want to pass it into
public DocumentDO toDocumentDO(FileItem fileItem);
Is there any way to do that or is it not possible (ie. the byte[] doesn't contain the metadata needed for it to be a FileItem like filename, mime type, etc)?
Your byte array is just what it is - bunch ow raw bytes, all meta data that you mentioned needs to be provided separately unless you read it into the file and then parse the file for the embedded meta information

Resources