How to change image resolution in android programmatically?

How to change image resolution in android programmatically?

There are numerous applications accessible on the net for performing picture tasks, for example, editing, decreasing picture record size, or resizing the pictures to specific aspects. The greater part of these applications are API based where the picture is transferred to mysterious servers, different capabilities are performed, and afterward shown accordingly accessible for download. The cycle becomes mind-boggling with the contribution of the web. In any case, there are not many applications that can locally perform comparative activities.



Through this article, we will show you how you could resize a picture to custom aspects automatically in Android.


Bit by bit Implementation

Stage 1: Create a New Project in Android Studio


To make another task in Android Studio kindly allude to How to Create/Start a New Project in Android Studio. We exhibited the application in Kotlin, so ensure you select Kotlin as the essential language while making a New Project.

Stage 2: Working with the activity_main.xml record


Explore to the application > res > design > activity_main.xml and add the beneath code to that document. The following is the code for the activity_main.xml document. Add two Buttons to transfer and resize and an ImageView to show the picture when transferred and resized in the format.

XML

-----------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">


<!-- Button to upload Image from the device -->

<Button

android:id="@+id/upload_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="Upload"/>


<!-- Button to perform resize operation -->

<Button

android:id="@+id/resize_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="Resize"

android:layout_alignParentRight="true"

android:layout_alignParentEnd="true" />


<!-- ImageView to display images after

uploading and resizing -->

<ImageView

android:id="@+id/imageView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"/>


</RelativeLayout>

-----------------------------------------------------------------------------------------------------------------------------

Stage 3: Working with the MainActivity.kt record


Go to the MainActivity.kt record and allude to the accompanying code. The following is the code for the MainActivity.kt document. Remarks are added inside the code to figure out the code in more detail. Perform resizing activities in the fundamental code.

Kotlin

-----------------------------------------------------------------------------------------------------------------------------

import android.app.Activity

import android.content.Intent

import android.graphics.Bitmap

import android.graphics.ImageDecoder

import android.net.Uri

import android.os.Build

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import android.provider.MediaStore

import android.widget.Button

import android.widget.ImageView

import android.widget.Toast

import androidx.annotation.RequiresApi

import java.io.IOException


class MainActivity : AppCompatActivity() {


// Declaring ImageView, number of images to pick

// from the device and a Bitmap to store the image

private lateinit var mImageView: ImageView

private val mPickImage = 1

private lateinit var mYourBitmap: Bitmap


override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)


// Declaring and initializing the buttons

val mUploadButton = findViewById<Button>(R.id.upload_button)

val mResizeButton = findViewById<Button>(R.id.resize_button)


// Initializing the image view

mImageView = findViewById(R.id.imageView)


// When upload button is clicked, the intent navigates

// to the local content in the device,

// where one can select the desired image

mUploadButton.setOnClickListener {

val intent = Intent(Intent.ACTION_GET_CONTENT)

intent.type = "image/*"

startActivityForResult(intent, mPickImage)

}


// When resize button is clicked

mResizeButton.setOnClickListener {

// Generate a new Bitmap of custom dimensions and set it in the image view

val resized = Bitmap.createScaledBitmap(mYourBitmap, 300, 300, true)

mImageView.setImageBitmap(resized)

}

}


@RequiresApi(Build.VERSION_CODES.P)

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

super.onActivityResult(requestCode, resultCode, data)

if (requestCode == mPickImage && resultCode == Activity.RESULT_OK) {

// If the image file does not exist

if (data == null) {

Toast.makeText(applicationContext,"Error",Toast.LENGTH_SHORT).show()

return

}


// Otherwise

try {

// Load the image address and set it in the image view

val imageUri: Uri = data.data!!

val source = ImageDecoder.createSource(this.contentResolver, imageUri)

mYourBitmap = ImageDecoder.decodeBitmap(source)

mImageView.setImageBitmap(mYourBitmap)

} catch (e: IOException) {

e.printStackTrace()

}

}

}

}

-----------------------------------------------------------------------------------------------------------------------------

How to change image resolution in android programmatically?

-----------------------------------------------------------------------------------------------------------------------------

public static Bitmap resizeImage(Bitmap realImage, float maxImageSize,

        boolean filter) {

    float ratio = Math.min(

            (float) maxImageSize / realImage.getWidth(),

            (float) maxImageSize / realImage.getHeight());

    int width = Math.round((float) ratio * realImage.getWidth());

    int height = Math.round((float) ratio * realImage.getHeight());


    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,

            height, filter);

    return newBitmap;

}

-----------------------------------------------------------------------------------------------------------------------------

Comments