Bitmap use of Android development

Source: Internet
Author: User

Bitmap is an important image processing tool class in Android that allows you to cut, rotate, scale, and manipulate images, and also to save image files by specifying format and compression quality.

First, get a bitmap object

View source We know that bitmap is final modified, so it is not possible to obtain an instance of bitmap by new, and to obtain a bitmap object is generally obtained through bitmapfactory, mainly in the following ways.

Each method has its own overloaded method, the overloaded method of the parameters are options, explain this parameter, this is mainly to generate bitmap object to do some simple processing.

inPreferredConfig 指定decode到内存中,手机中所采用的编码,可选值定义在Bitmap.Config中。缺省值是ARGB_8888。inJustDecodeBounds 如果设置为true,并不会把图像的数据完全解码,亦即decodeXyz()返回值为null,但是Options的outAbc中解出了图像的基本信息。inSampleSize 设置decode时的缩放比例。当值大于1时会对图像进行压缩,比如设置为4时,返回的图像宽高都将是原始图像的四分之一,像素是原始图像的十六分之一,如果设置的值小于1,则会被当作1来处理。注意,这里设值必须是2的次幂,其他值会被就近取离2的次幂最近的值。

By the option of these properties, we can get a thumbnail image of a picture, a basic idea is to set Injustdecodebounds to True, get the basic information of the image, such as width and height, according to the basic information combined with the thumbnail target width, Figure out the Insamplesize, and then set Injustdecodebounds to False to get the thumbnail.

To get a bitmap object, you can get a bitmap object through the full path of an image, either through a byte stream or through a bytes or through a local resource file.

if (Environment. Getexternalstoragestate(). Equals(Environment. MEDIA_mounted)) {///storage/emulated/0/dcim/camera/123.JPG String Path = Environment. getExternalStorageDirectory(). GetAbsolutePath() + File. Separator+"DCIM"+ File. Separator+"Camera"+ File. Separator+"123.jpg";Bitmapfactory. Optionsoption = new Bitmapfactory. Options();Option. Insamplesize=1;Get the Bitmap object from the full path Bitmap BM = Bitmapfactory. DecodeFile(path,option);FileInputStream is = new FileInputStream (new File (path));Get the Bitmap object through an input stream Bitmap bm2 = bitmapfactory. Decodestream(IS);Get a Bitmap object from a local resource Bitmap BM1 = bitmapfactory. Decoderesource(Getresources (), R. drawable. IC_launcher);iv. Setimagebitmap(BM2);}

Second, save the image file

Get the bitmap can be displayed directly after, you can save to the local waiting for processing, bitmap can be saved as jpg,png,gif and other formats, save the way is very simple:

Bitmap bp = revitionimagesize (path +"123.jpg");FileOutputStream out= new FileOutputStream (path +"456.gif");//0means to compress to a minimum, -means the best quality after compression, PNG is lossless image, will ignore this parameter//BP. Compress(Bitmap. Compressformat. JPEG, -, out);Bp. Compress(Bitmap. Compressformat. PNG,0, out);                 out. Flush();                 out. Close();

Call the Compress method directly, the first parameter represents the way of compression, there are many, PNG belongs to lossless compression, JPEG is lossy compression. The second parameter represents the compression quality, and the value of 0-100,100 indicates that the compressed image has the highest quality, but at the same time the compression rate is low, 0 means the compression image is the worst and the compression rate is high. However, whether this parameter is valid is related to the first parameter, if PNG is selected for the first parameter, and lossless compression, then the second parameter is invalid. The third parameter is the byte stream you want to output.

This article references

This article source code download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Bitmap use of Android development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.