How to upgrade the quality of a Blob image after SQLite - java

The Encode source i have to put these Blobs (images) in my database is
Bitmap image15 = BitmapFactory.decodeResource(getResources(),R.drawable.wolverineklein);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image15.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte1[] = stream.toByteArray();
And this is the code to get the image back from the SQLite database
DatabaseHandler db = new DatabaseHandler(camera.this);
List<Database> contacts = db.getAllContacts();
for (Database contact : contacts) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
//bmpFactoryOptions.inScaled = false;
// decodes the blob back into a bitmap
byte[] blob = contact.getMP();
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Bitmap scalen = Bitmap.createScaledBitmap(bitmap, 320, 240, false);
The code works fine, but the output of the images is of a lower quality (the difference isn't that big, but i use it for feature matching so it does impact the results. The right output image is more blurry then the )
As you can see, the right image is the output and it is more bit-like (check out the upper chest area and compare it to the left input image) then the original + the size is bigger. Is there a possibility to make the quality better then I have it right now?

Related

Converting PNG to Bitmap to Byte[] to store in SQL

I'm trying to convert a PNG file into a Bitmap into a Byte[] so I can store the image in my SQL database.
Converting the PNG to a Bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.MyPicture, options)
Convert Bitmap to Byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bArray = stream.toByteArray();
Insert into database
MainViewModel myViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
MyEntity myEntity = new MyEntity(bArray);
myViewModel.insert(myEntity);
I'm Currently getting NullpointException, in the b.compress line of my code.
Thanks for any and all help.
your bitmap is null because you have set options.inJustDecodeBounds = true and passing this options variable to decodeResource() function. Read these docs

Get Image From Image view and send This image to other fragment with encode bitmap

i want get image From server and put to this image view i want get image from this imageview and compress with bitmap and send this picture to other fragment i dont want add 2 request for my server
my code is :
Bitmap bmp = BitmapFactory.decodeResource(getResources(),"my problem");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
mBundle.putByteArray("ResID", byteArray);
i know if i put R.darwable.ic_luncher it will work
but i want take this image from image view and put to " my probem "
can u help me ?
You can use mImageView.getDrawable() to get Drawable object from ImageView:
Drawable drawable = mImageView.getDrawable();
After that you can obtain a Bitmap object from it:
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable.getBitmap();
and further, if you want, even InputStream for byte array to send it:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Here, the CompressFormat and quality can be adjusted for output image size
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
As a result, imageBytes will contain all the bytes of the image, which you can send over elsewhere (in your scenario: to your other Fragment)

How to crop image to a set number of bytes?

I am coding an app where I take a picture and I pass the picture into an image recognition model. The problem is, the recognition only takes 150528 bytes, and the number of bytes in the image depends on the image quality. Is there a way in Java to crop the top and bottom of the image as evenly as possible depending on the quality to make it a set number of bytes each time?
This is my code for this part so far.
int width = 100;
int height = 200;
final ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
outputSurface.add(new Surface(textureView.getSurfaceTexture()));
As you can see, I'm restricting the size of the image captured, but the number of bytes depends on the quality so I need to actually just crop the image. How can I do this?
You can crop/ resize image with the following
byte[] thumb_byte_data;
Uri resultUri = ImageUri;
//getting imageUri and store in file. and compress to bitmap
File file_path = new File(resultUri.getPath());
try {
Bitmap thumb_bitmap = new Compressor(this)
.setMaxHeight(200)
.setMaxWidth(200)
.setQuality(75)
.compressToBitmap(file_path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumb_byte_data = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}

converting from byte array to drawable image gives corrupt image: Android

I am converting an image into a byte array. Sending it over a socket, then once it is through the socket, I need to convert it back to a drawable, png, or any type of image that I can use as the background of an image button. The problem is that when I either convert into a byte array, or from an array into a drawable, the file is getting corrupted.
I am getting my original image from the last installed app on my phone as follows and then I am saving it to a file on the phone so I can check that at this point the image file is successfully captured (and it is. Nothing is corrupt at this point):
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
BitmapDrawable bitmapIcon = (BitmapDrawable)icon;
FileOutputStream fosIcon = context.openFileOutput(applicationName + ".png", Context.MODE_PRIVATE);
bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
InputStream inputStream = context.openFileInput(applicationName + ".png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// GET FILE DIRECTORY
File imageFile = new File(context.getFilesDir(), applicationName + ".png");
Now I am converting this bitmap to a byte array to send over the socket:
// get bitmap image in bytes to send
int bytes = bitmap.getByteCount();
Log.d("tag_name", "Number of Bytes" + bytes);
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
bitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
Log.d("tag_name", "array" + array);
int start=0;
int len=array.length;
Log.d("tag_name", "length" + len);
new SendToClient(array, applicationName, len, start).execute();
At this point I know my file is successfully getting saved as this image:
Then, in SendToClient, I am using DataOutputStream to send over the array. I am not posting this code, because I have tested that sending the data is not where the problem occurs. If I don't send the byte array, and convert from the byte array back to a drawable in this same activity, it is also corrupt.
Here is how I convert from an array back to a drawable after using DataInputStream to read the array that was sent:
YuvImage yuvimage=new YuvImage(array, ImageFormat.NV21, 100, 100, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, 100, 100), 80, baos);
byte[] jdata = baos.toByteArray();
// Convert to Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
Log.d("tag_name", "bmp" + bmp);
// convert bitmap to drawable
final Drawable d = new BitmapDrawable(context.getResources(), bmp);
The reason I am first compressing to a JPEG is because if I only used BitmapFactory.decodeByteArray, then I would get "bmp = null", converting to JPEG first was the only solution I found where I could get a bitmap that wasn't null, but now my image is corrupt. This is what Drawable d looks like:
Try the following code,
Convert Bitmap to ByteArray.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.myImage);
ByteArrayOutputStream opstream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, opstream);
byte[] bytArray = opstream.toByteArray();
Convert ByteArray to Bitmap.
Bitmap bitmap = BitmapFactory.decodeByteArray(bytArray, 0, bytArray.length);
ImageView img = (ImageView) findViewById(R.id.imgv1);
img.setImageBitmap(bitmap);
This may helps you.

Image to base64

How can I convert all the images stored in one folder to base64 data? in the following code tFotos is the folder where all my images are stored.
ArrayList<String> mList = new ArrayList<String>();
Bitmap selectedImage = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory()+"tFotos");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100,
stream);
byte[] byteArray = stream.toByteArray();
String strBase64 = Base64.encodeToString(byteArray, 0);
mList.add(StrBase64);
#Ruben JG
You have to provide full path of image like sdcard/foldername/filename.PNG/.JPEG

Categories

Resources