Skip to main content

Cropping , Scaling and Rotating image in Android

Here i am not writing any program to crop, scale and rotate image , im just writing the basic process with some code to show it...

Cropping of BitmapImage is done in an easier way as follows
Get the bitmap image -
if its in resources

Bitmap origBitmap=BitmapFactory.decodeResource(getResource(),R.drawable.bitmapImage);

now u have got the Bitmap image from the drawable resource, my drawable resource name is bitmapImage.png , u can choose your own and correct accordingly.

Co-ordinates start from the top left corner of the Image as in Java.
So if u want to crop from (0,0) having width 50 and height 50 of an image having width and height of 400*400.

u jsut have to create a new Bitmap by giving appropriate coordinates,,

Bitmap croppedBitmap=Bitamp.create(origBitmap,0,0,50,50);

here u have to consider the width and height of the cropped bitmap that it should not exceed the original height and width of the image.

now Your Cropped Bitmap Image is ready , you can use it in imageView
as

imageView.setBitmap(croppedBitmap);

Generally cropping is not sufficient to covert a large image to small image, it should be scaled.

Scaling of image
Here i m using cropped image for scaling , u can use any image.
Android has provided a method to scale bitmap into a new bitmap

just use
Bitmap
scaledBitmap=Bitmap.createScaledBitmap(croppedBitmap,targetWidth,targetHeight,true);

Rotating Bitamps

to rotate a bitmap u also have to create a new Bitmap , but u need an extra param matrix
create a new matrix
Matrix matrix = new Matix();
matrix.postRotate(90);

here i have used 90 degrees rotation , you can use your own,
now just create new Bitmap by using this matrix,
Bitmap rotatedBitmap=Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),true);

Thus it creates a rotated bitmap of our scaled Bitmap .. ,, now u can use any operation of your choice.

Comments

  1. Nice code Atul,i have a similar issue. I want to crop a bitmap by a freehand area drawn by dragging fingers over screen. can you help me, any help would be appreciated

    ReplyDelete

Post a Comment

Popular posts from this blog

Database Sharding

Collating some of the resources which talks about Database Sharding. https://en.wikipedia.org/wiki/Shard_(database_architecture) [Feb 2019]  http://highscalability.com/blog/2019/2/19/intro-to-redis-cluster-sharding-advantages-limitations-deplo.html Redis Cluster is the Native Sharding implementation available within Redis that allows your to automatically distribute your data across multiple nodes without having to rely on external tools and utilities. Its covers Sharding with Redis Cluster  where Redis Clusters is divided in 16384 slots and these slots are assigned to multiple Redis Nodes. The  Redis Cluster Specification  is the definitive guide to understanding the internals of the technology, while the  Redis Cluster Tutorial  provides deployment and administration guidelines. [ Jan 2019  ]  https://scalegrid.io/blog/scalegrid-hosting-adds-support-for-highly-available-redis-clusters-with-automated-sharding/ ScaleGrid : Fully Manage...

Penetration Testing Basics

Penetration testing, often called “pentesting”,“pen testing”, or “security testing”, is the practice of attacking your own or your clients’ IT systems in the same way a hacker would to identify security holes. Of course, you do this without actually harming the network. The person carrying out a penetration test is called a penetration tester or pentester. The difference between Penetration Testing and Hacking is that you have the system owner's permission to do testing and to identfiy security holes. If you want to do penetration testing u should better ask for his/her permission. Basic Security Concepts Vulnerability: It is a security hole in a piece of software, hardware of Operating system that provides a way to attack the system.A vulnerabilty is as simple as weak passwords and as complex as buffer overflows as well as SQL injection. Security Research: Vulnerabilities are typically searched by security researchers who finds the flaws in the system. Security Research can ...