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

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

Related

Compress image before uploading to Firebase

Before I was using MVVM architecture and Kotlin I done it like this in Java.
I am passing image Uri to Bitmap
Java code:
Bitmap actualImage1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
actualImage1.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] finalImage = baos.toByteArray();
getContentResolver() is not recognized by Kotlin.
How to write this in Kotlin, again I have image uri that I want to pass to Bitmap
I do it like this in Kotlin
val bitmap = MediaStore.Images.Media.getBitmap(
this.contentResolver,
selectedPhotoUri
)

Trying to display image retrived from server with base64 encoding but cant display the correct image

These are my encoding and decoding methods.
public String encodeBase64(Bitmap bit){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.PNG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP
byte[] bai = baos.toByteArray();
return Base64.encodeToString(bai, Base64.DEFAULT);
}
public Bitmap decodeBase64(String base64Image){
byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap bm;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
return bm;
}
This is encoded data from a bitmap
iVBORw0KGgoAAAANSUhEUgAABLAAAAdcCAYAAAC7T3KrAAAABHNCSVQICAgIfAhkiAAAIABJREFU
eJzswQEBAAAAgJD+r+4ICgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.... etc
But the data i get from the server looks like this
{"type":"Buffer","data":[105,86,66,79,82,119,48,75,71,103,111,65,65,65,... etc
I can get pure data or the type and data json object. Ive tried to decode both. When i try to decode with the type and data it gives me a null bitmap, when i decode the raw data only the image is not displayed, but nothing seems to crash.
Do i need to retrive all those array numbers and convert them to chars before decoding? Ive read other post and it sounded very straigtforward, just encode and then decode.
decode base_64 string Imgae in codigneter
$
aadharcard_front = image name(database)
if(!empty($aadharcard_front))
{
$aadharcard_front = base64_decode($aadharcard_front);
$aadharcard_front_time = time().'.png';
file_put_contents(APPPATH.'../media/uploads/'.$aadharcard_front_time,$aadharcard_front);
$user_data['aadharcard_front'] = $aadharcard_front_time;
}

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)

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

How to upgrade the quality of a Blob image after SQLite

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?

Categories

Resources