Android image processing,
I believe that our partners in Android development are very impressed with Android image compression and cropping. Today, I will lead you to learn this profound and unpredictable knowledge, so that you can learn and work in the future.
First, let's take a look at the cause of this problem. When we need to add a large number of images to our application, the program will often report the OMM problem. What is the problem? When we add too many photos or too many photos, Android will report an out-of-memory error. We often encounter this problem helpless. Image Cache Optimization can help us solve this problem, however, many times we don't know how to optimize the memory. Obviously, I am like this. Here I use the compression and image cropping methods to reduce the memory occupied by images, this allows you to smoothly display images.
Next, let's take a look at how to crop the image: There are two ways to crop the image, one is to call the system's cropping method, this method allows users to choose their own cropping area, many videos about image cropping are about this. The second is to crop images directly through the background function. The advantage of this method is that it simplifies user operations and is convenient and convenient. For the first type, you can refer to Baidu. In this article, we will discuss how to use background functions to automatically crop images.
File file = new File (fileString); // file object; fileString: File address InputStream is = null; try {is = new FileInputStream (file ); // get a file input stream object} catch (FileNotFoundException e) {e. printStackTrace ();} BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = false; // This parameter must be set to false. If it is set to true, null is returned in decode, this setting allows you to query the attributes of a bitmap, such as the length and width of the bitmap, without occupying the memory size double n = file. length ()/1024.0; // obtain the object size if (n <200) {options. inSampleSize = 2; // width, hight is set to the original binary one} else {options. inSampleSize = 3; // width, hight is set to the original one} Bitmap bitmap = BitmapFactory. decodeStream (is, null, options );
BitmapFactory. decodeStream (is, null, options); Based on the above, we set the compression ratio to compress the image.
Next, let's talk about how to crop an image. Here we will introduce custom cropping. We can crop an image by setting the cropping start point and the cropping length and width. Here, we will crop the edges centered by the shortest side based on the length and width of the image.
Int w = options. outWidth; // get the image width int h = options. outHeight; // get the Image Height int wh = w> h? H: w; // The side length of the square area obtained after cropping int retX = w> h? (W-h)/2: 0; // based on the source image, take the x coordinate int retY = w> h? 0: (h-w)/2; // based on the source image, take the y coordinate Bitmap. createBitmap (bitmap, retX, retY, wh, wh, null, false) in the upper left corner of the square );
Now, we have finished the compression and cropping of images. I hope it will be helpful to you. ImageView screen and image cropping function video download: http://pan.baidu.com/s/1mhkM1s8