Android 5: processing bitmap

Source: Internet
Author: User

Image processing includes the following:
1. Zoom
2. skew
3. Rotate
4. Zoom +
5. Translation
6. Images
The Code is as follows:
 
1. package com. mike. activity;
2.
3. import java. io. FileNotFoundException;
4.
5. import android. app. Activity;
6. import android. content. Intent;
7. import android. graphics. Bitmap;
8. import android. graphics. BitmapFactory;
9. import android. graphics. BitmapFactory. Options;
10. import android. graphics. Canvas;
11. import android. graphics. Color;
12. import android. graphics. Matrix;
13. import android. graphics. Paint;
14. import android.net. Uri;
15. import android. OS. Bundle;
16. import android. util. Log;
17. import android. view. Display;
18. import android. view. View;
19. import android. view. View. OnClickListener;
20. import android. widget. Button;
21. import android. widget. ImageView;
22.
23. public class ImageDemoActivity extends Activity implements OnClickListener {
24./** Called when the activity is first created .*/
25.
26. public static String TAG = "IMAGE ";
27. public static int REQUEST_CODE = 0;
28. private ImageView mImageShow;
29. private ImageView mImageAltered;
30.
31. @ Override
32. public void onCreate (Bundle savedInstanceState ){
33. super. onCreate (savedInstanceState );
34. setContentView (R. layout. main );
35.
36. Button imageSelectBtn = (Button) findViewById (R. id. imageSelectBtn );
37. mImageShow = (ImageView) findViewById (R. id. imageShow );
38. mImageAltered = (ImageView) findViewById (R. id. imageAltered );
39. imageSelectBtn. setOnClickListener (this );
40 .}
41.
42. public void onClick (View v ){
43. // TODO Auto-generated method stub
44. Intent intent = new Intent (Intent. ACTION_PICK,
45. android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI); // start the Photo Gallery
46. startActivityForResult (intent, 0 );
47 .}
48.
49. @ Override
50. protected void onActivityResult (int requestCode, int resultCode,
51. Intent intent ){
52. // TODO Auto-generated method stub
53. super. onActivityResult (requestCode, resultCode, intent );
54.
55. if (resultCode = RESULT_ OK) {// operation successful
56. Uri imgFileUri = intent. getData (); // obtain the information of the selected photo
57. Log. d (TAG, "imgFileUri is:" + imgFileUri );
58. // The returned image may be too large to be fully loaded into the memory. The system has restrictions and needs to be processed.
59. Display currentDisplay = getWindowManager (). getdefadisplay Display ();
60. int defaultHeight = currentDisplay. getHeight ();
61. int defaultWidth = currentDisplay. getWidth ();
62.
63. BitmapFactory. Options bitmapFactoryOptions = new BitmapFactory. Options ();
64. bitmapFactoryOptions. inJustDecodeBounds = false; // only obtains the size of the original image without returning the Bitmap object.
65. // Note: If set to true, the decoder will return null (no bitmap),
66. // the out... fields will still be set,
67. // allowing the caller to query the bitmap without having
68. // allocate the memory for its pixels
69. try {
70. Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver ()
71 .. openInputStream (imgFileUri), null,
72. bitmapFactoryOptions );
73. int outHeight = bitmapFactoryOptions. outHeight;
74. int outWidth = bitmapFactoryOptions. outWidth;
75. int heightRatio = (int) Math. ceil (float) outHeight
76./defaultHeight );
77. int widthRatio = (int) Math. ceil (float) outWidth
78./defaultWidth );
79.
80. if (heightRatio> 1 | widthRatio> 1 ){
81. if (heightRatio> widthRatio ){
82. bitmapFactoryOptions. inSampleSize = heightRatio;
83.} else {
84. bitmapFactoryOptions. inSampleSize = widthRatio;
85 .}
86 .}
87.
88. bitmapFactoryOptions. inJustDecodeBounds = false;
89. bitmap = BitmapFactory. decodeStream (getContentResolver ()
90 .. openInputStream (imgFileUri), null,
91. bitmapFactoryOptions );
92.
93. mImageShow. setImageBitmap (bitmap );
94.
95 .///*
96. // * draw a bitmap on the in-place Graph
97 .//*/
98 .//
99. // Bitmap bitmapAltered = Bitmap. createBitmap (bitmap. getWidth (),
100. // bitmap. getHeight (), bitmap. getConfig ());
101 .//
102. // Canvas canvas = new
103. // Canvas (bitmapAltered); // bitmap provides the Canvas, only the size and size are provided here, and no background is displayed after the offset.
104 .//
105 .//
106. // Paint paint = new Paint ();
107 .//
108. // canvas. drawBitmap (bitmap, 0, 0, paint); // The image is exactly the same as the previous one.
109 .//
110. // mImageAltered. setImageBitmap (bitmapAltered );
111.
112 ./*
113. * use Matrix
114 .*/
115.
116. Bitmap bitmapAltered = Bitmap. createBitmap (bitmap. getWidth (),
117. bitmap. getHeight (), bitmap. getConfig (); // The disadvantage is to limit the size of the canvas. The processed image may be truncated. You can introduce a matrix Construction Method to dynamically set the size of the canvas www.2cto.com.
118. Canvas canvas = new Canvas (bitmapAltered); // bitmap provides the Canvas, only the size and size are provided here, and no background is displayed after the offset,
119. Paint paint = new Paint ();
120.
121. Matrix matrix = new Matrix ();
122.
123. // 1: Zoom
124. // matrix. setValues (new float [] {// basic knowledge of linear algebra that can be multiplied
125. //, 0, // calculate the value of x
126. // 0, 0, // calculate the value of y
127. //, 0, 1 // the two-dimensional graph is not used
128 .//});
129.
130. // 2: Tilt
131. // matrix. setValues (new float [] {// basic knowledge of linear algebra that can be multiplied
132. // 1,. 5f, 0, // calculate the value of x, which is related to the value of x and y.
133. // 0, 0, // calculate the value of y
134. //, 0, 1 // the two-dimensional graph is not used
135 .//});
136.
137. // 3: Rotate
138. // matrix. setRotate (45); // sets the rotation angle, (0, 0), and rotates 45 degrees clockwise.
139.
140. // 4, zoom plus: Use the Matrix class method // you need to modify the canvas width> 1.5 x the original width.
141. // matrix. setScale (1.5f, 1 );
142.
143. // 5, pan
144. // matrix. setTranslate (10, 0 );
145.
146. // 6, Image
147. // eg1: X axis Image
148. // matrix. setScale (-1, 1); // draw to the left. The original image is symmetric along the Y axis.
149. // matrix. postTranslate (bitmap. getWidth (), 0); // note that post is followed by moving
150. // eg2: Y axis Image
151. matrix. setScale (1,-1); // draw up. The original image is symmetric along the X axis and the image is in the fourth quadrant.
152. matrix. postTranslate (0, bitmap. getHeight (); // note that post is post moving
153.
154.
155 .//~ Optimize ~ Because bitmapAltered is specified for the canvas, the processed image may be truncated. The bitmapAltered size can be dynamically changed through the matrix.
156. // eg: matrix. setRotate (degrees, px, py)
157. // bitmapAltered = Bitmap. createBitmap (source, x, y, width, height, m, filter); // apparently the matrix affects its size
158.
159 .//
160.
161. // Note: for more details: Wikipedia Transformation Matrix
162. // http://en.wikipedia.org/wiki/Transformation_matrix
163.
164. canvas. drawBitmap (bitmap, matrix, paint); // The image is exactly the same as the previous one.
165.
166. mImageAltered. setImageBitmap (bitmapAltered );
167.
168.} catch (FileNotFoundException e ){
169. // TODO Auto-generated catch block
170. e. printStackTrace ();
171 .}
172.
173 .}
174 .}
175 .}

 
From xiaoxin's column

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.