CHAPTER3 ( ) Gallery 67
68 CHAPTER 3 Intent ACTION_PICK URI android provier MediaStore Images Media EXTERNAL_CONTENT_URI URI SD MediaStore Intent choosepictureintent = new Intent(Intent.ACTION_PICK, ë android.provider.mediastore.images.media.external_content_uri); Gallery onactivityresult URI onactivityresult(int requestcode, int resultcode, Intent intent) { super.onactivityresult(requestcode, resultcode, intent); if (resultcode == RESULT_OK) { Uri imagefileuri = intent.getdata(); package com.apress.proandroidmedia.ch3.choosepicture; import java.io.filenotfoundexception; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.display;
69 import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; OnClickListener oncreate UI findviewbyid UI XML public class ChoosePicture extends Activity implements OnClickListener { ImageView chosenimageview; Button choosepicture; @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); chosenimageview = (ImageView) this.findviewbyid(r.id.chosenimageview); choosepicture = (Button) this.findviewbyid(r.id.choosepicturebutton); choosepicture.setonclicklistener(this); onclick choosepicture 3 1 Gallery Gallery 3 2
70 CHAPTER 3 public void onclick(view v) { Intent choosepictureintent = new Intent(Intent.ACTION_PICK, ë android.provider.mediastore.images.media.external_content_uri); startactivityforresult(choosepictureintent, 0);
71 Gallery onactivityresult URI protected void onactivityresult(int requestcode, int resultcode, Intent intent) { super.onactivityresult(requestcode, resultcode, intent); if (resultcode == RESULT_OK) { Uri imagefileuri = intent.getdata(); 1 dw int dh int
72 CHAPTER 3 Display currentdisplay = getwindowmanager().getdefaultdisplay(); int dw = currentdisplay.getwidth(); int dh = currentdisplay.getheight() / 2-100; try { //. BitmapFactory.Options bmpfactoryoptions = new BitmapFactory.Options(); bmpfactoryoptions.injustdecodebounds = true; Bitmap bmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); int heightratio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float) dh); int widthratio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float) dw); if (heightratio > 1 && widthratio > 1) { if (heightratio > widthratio) { bmpfactoryoptions.insamplesize = heightratio; else { bmpfactoryoptions.insamplesize = widthratio; bmpfactoryoptions.injustdecodebounds = false; bmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); chosenimageview.setimagebitmap(bmp); catch (FileNotFoundException e) { Log.v( ERROR, e.tostring()); XML layout/main xml
73 <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_parent android:layout_height= fill_parent > <Button android:layout_width= fill_parent android:layout_height= wrap_content android:text= Choose Picture android:id= @+id/choosepicturebutton /> <ImageView android:layout_width= wrap_content android:layout_height= wrap_content android:id= @+id/chosenimageview ></ImageView> </LinearLayout>
74 CHAPTER 3 Bitmap Bitmap 3 3 Bitmap Bitmap Bitmap Bitmap Bitmap 1 BitmapFactory decodestream Bitmap bmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap alteredbitmap = Bitmap.createBitmap(bmp.getWidth(), ë bmp.getheight(), bmp.getconfig()); alteredbitmap Bitmap bmp ( color depth) Bitmap Config Bitmap createbitmap (mutable) Bitmap (mutable) Bitmap
75 (immutable) Bitmap Bitmap Canvas Canvas Canvas Bitmap Canvas canvas = new Canvas(alteredBitmap); Paint Paint Paint Paint paint = new Paint(); Bitmap (mutable) Bitmap Bitmap bmp = BitmapFactory.decodeStream(getContentResolver(). openinputstream(imagefileuri), null, bmpfactoryoptions); Bitmap alteredbitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getheight(), bmp.getconfig()); Canvas canvas = new Canvas(alteredBitmap); Paint paint = new Paint(); canvas.drawbitmap(bmp, 0, 0, paint); ImageView alteredimageview = (ImageView) this.findviewbyid(r.id.alteredimageview); alteredimageview.setimagebitmap(alteredbitmap);
76 CHAPTER 3 Canvas drawbitmap Paint Bitmap x y alteredbitmap Choose Picture onactivityresult bmp = BitmapFactory decodestream import alteredbitmap ImageView alteredbitmap setimagebitmap XML id AlteredImageView ImageView XML 3 4 alteredbitmap ImageView ImageView <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_parent android:layout_height= fill_parent > <Button android:layout_width= fill_parent android:layout_height= wrap_content android:text= Choose Picture android:id= @+id/choosepicturebutton /> <ImageView android:layout_width= wrap_content android:layout_height= wrap_content ë android:id= @+id/chosenimageview ></ImageView> <ImageView android:layout_width= wrap_content android:layout_height= wrap_content ë android:id= @+id/alteredimageview ></ImageView> </LinearLayout>
77 / / API Matrix Bitmap Bitmap Bitmap
78 CHAPTER 3 (spatial transformation) / Matrix 9 Matrix Matrix Matrix (x y z) Matrix 9 1 0 0 0 1 0 0 0 1 (1 0 0) x=1x+0y+0z x x x y z (0 1 0) y y=0x+1y+0z (0 0 1) z z = 0x +0y + 1z Matrix Matrix setvalues Matrix
79 Matrix matrix = new Matrix(); matrix.setvalues(new float[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 ); drawbitmap canvas.drawbitmap(bmp, matrix, paint); Matrix 1 5 3 5 x 50% x x.5 0 0 0 1 0 0 0 1 Matrix matrix = new Matrix(); matrix.setvalues(new float[] {.5f, 0, 0, 0, 1, 0, 0, 0, 1 ); canvas.drawbitmap(bmp, matrix, paint);
80 CHAPTER 3 x y Matrix matrix = new Matrix(); matrix.setvalues(new float[] { 1,.5f, 0, 0, 1, 0, 0, 0, 1 ); canvas.drawbitmap(bmp, matrix, paint);
81 3 6 x y y x 3 7 alteredbitmap = Bitmap.createBitmap(bmp.getWidth()*2,bmp.getHeight(),bmp.getConfig());
82 CHAPTER 3 Matrix Matrix (Wikipedia Transformation Matrix) http://en wikipedia org/wiki/ Transformation_matrix Matrix
83 Matrix canvas drawbitmap setrotation setrotation float (0 0) 3 8 Matrix matrix = new Matrix(); matrix.setrotate(15); canvas.drawbitmap(bmp, matrix, paint);
84 CHAPTER 3 setrotation 3 9 matrix.setrotate(15,bmp.getwidth()/2,bmp.getheight()/2); Matrix setscale setscale / float x y 3 10 setscale
85 matrix.setscale(1.5f,1); Matrix settranslate (translate) x y settranslate float x y x y x y
86 CHAPTER 3 settranslate(1.5f,-10); prescale setrotate setscale postrotate 3 11 matrix.setscale(1.5f, 1); matrix.postrotate(15,bmp.getwidth()/2,bmp.getheight()/2);
87 setscale posttranslate ( ) / 0 0 x posttranslate 3 12 matrix.setscale(-1, 1); matrix.posttranslate(bmp.getwidth(),0);
88 CHAPTER 3 y 180 3 13 matrix.setscale(1, -1); matrix.posttranslate(0, bmp.getheight());
89 Bitmap Bitmap Bitmap Matrix Canvas Paint Bitmap Bitmap createbitmap Bitmap x y Matrix Matrix matrix = new Matrix(); matrix.setrotate(15,bmp.getwidth()/2,bmp.getheight()/2); alteredbitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getwidth(), bmp.getheight(), matrix, false); alteredimageview.setimagebitmap(alteredbitmap); Bitmap(alteredBitmap) (bmp) Matrix Bitmap / Bitmap
90 CHAPTER 3 / Canvas Matrix ColorMatrix Canvas Paint
91 ColorMatrix Matrix ColorMatrix x y z Red Green Blue Alpha ColorMatrix ColorMatrix cm = new ColorMatrix(); ColorMatrix Canvas ColorMatrix ColorMatrixColorFilter Paint paint.setcolorfilter(new ColorMatrixColorFilter(cm)); Choose Picture ColorMatrix Bitmap bmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); Bitmap alteredbitmap = Bitmap.createBitmap(bmp.getWidth(), ë bmp.getheight(),bmp.getconfig()); Canvas canvas = new Canvas(alteredBitmap); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); paint.setcolorfilter(new ColorMatrixColorFilter(cm)); Matrix matrix = new Matrix(); canvas.drawbitmap(bmp, matrix, paint); alteredimageview.setimagebitmap(alteredbitmap); chosenimageview.setimagebitmap(bmp);
92 CHAPTER 3 ColorMatrix (identity) Matrix 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 float 20 128 128 128 0 ( ) New Red Value = 1*128 + 0*128 + 0*128 + 0*0 + 0 New Blue Value = 0*128 + 1*128 + 0*128 + 0*0 + 0 New Green Value = 0*128 + 0*128 + 1*128 + 0*0 + 0 New Alpha Value = 0*128 + 0*128 + 0*128 + 1*0 + 0 128 1 (0)
93 2 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 ColorMatrix cm = new ColorMatrix(); cm.set(new float[] { 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 ); paint.setcolorfilter(new ColorMatrixColorFilter(cm)); (intensity) 3 15 ColorMatrix cm = new ColorMatrix(); float contrast = 2; cm.set(new float[] { contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 ); paint.setcolorfilter(new ColorMatrixColorFilter(cm));
94 CHAPTER 3 (color intensity) ColorMatrix cm = new ColorMatrix(); float brightness = -25; cm.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 ); paint.setcolorfilter(new ColorMatrixColorFilter(cm));
95 ColorMatrix cm = new ColorMatrix(); float contrast = 2; float brightness = -25; cm.set(new float[] { contrast, 0, 0, 0, brightness, 0, contrast, 0, 0, brightness, 0, 0, contrast, 0, brightness, 0, 0, 0, contrast, 0 ); paint.setcolorfilter(new ColorMatrixColorFilter(cm)); 3 16
96 CHAPTER 3 ColorMatrix (saturation) ColorMatrix cm = new ColorMatrix(); cm.setsaturation(.5f); paint.setcolorfilter(new ColorMatrixColorFilter(cm)); 1 (0) 1 (0) (grayscale) SDK Canvas Bitmap Bitmap Canvas Paint transfermode(xfermode) transfermode Xfermode PorterDuffXfermode (Thomas Porter) (Tom Duff) 1984 ACM SIGGRAPH PorterDuff Mode
97 : Paint : SRC DST : : : : : : : : :
98 CHAPTER 3 : : : 255 = / 255 : ( 255 ) = 255 (((255 ) (255 )) / 255) package com.apress.proandroidmedia.ch3.choosepicturecomposite; import java.io.filenotfoundexception; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.paint; import android.graphics.porterduffxfermode; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.display; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; public class ChoosePictureComposite extends Activity implements OnClickListener {
99 Choose Picture Composite Button OnClickListener Button Button Button static final int PICKED_ONE = 0; static final int PICKED_TWO = 1; boolean onepicked = false; boolean twopicked = false; Button choosepicture1, choosepicture2; ImageView Bitmap ImageView compositeimageview; Bitmap bmp1, bmp2; Canvas Paint Canvas canvas; Paint paint; @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); compositeimageview = (ImageView) this.findviewbyid(r.id.compositeimageview);
100 CHAPTER 3 choosepicture1 = (Button) this.findviewbyid(r.id.choosepicturebutton1); choosepicture2 = (Button) this.findviewbyid(r.id.choosepicturebutton2); choosepicture1.setonclicklistener(this); choosepicture2.setonclicklistener(this); Button OnClickListener onclick View Button which Button Gallery Gallery ACTION_PICK public void onclick(view v) { int which = -1; if (v == choosepicture1) { which = PICKED_ONE; else if (v == choosepicture2) { which = PICKED_TWO; Intent choosepictureintent = new Intent(Intent.ACTION_PICK, ë android.provider.mediastore.images.media.external_content_uri); startactivityforresult(choosepictureintent, which); onactivityresult startactivityforresult requestcode
101 Bitmap protected void onactivityresult(int requestcode, int resultcode, Intent intent) { super.onactivityresult(requestcode, resultcode, intent); if (resultcode == RESULT_OK) { Uri imagefileuri = intent.getdata(); if (requestcode == PICKED_ONE) { bmp1 = loadbitmap(imagefileuri); onepicked = true; else if (requestcode == PICKED_TWO) { bmp2 = loadbitmap(imagefileuri); twopicked = true; Bitmap Bitmap bmp1 (mutable) Bitmap Canvas Paint Bitmap(bmp1) Paint transfermode PorterDuffXfermode Bitmap Canvas ImageView Bitmap MULTIPLY if (onepicked && twopicked) { Bitmap drawingbitmap = Bitmap.createBitmap(bmp1.getWidth(), ë bmp1.getheight(), bmp1.getconfig()); canvas = new Canvas(drawingBitmap); paint = new Paint(); canvas.drawbitmap(bmp1, 0, 0, paint); paint.setxfermode(new PorterDuffXfermode(android.graphics. ë
102 CHAPTER 3 PorterDuff.Mode.MULTIPLY)); canvas.drawbitmap(bmp2, 0, 0, paint); compositeimageview.setimagebitmap(drawingbitmap); 1 Bitmap URI private Bitmap loadbitmap(uri imagefileuri) { Display currentdisplay = getwindowmanager().getdefaultdisplay(); float dw = currentdisplay.getwidth(); float dh = currentdisplay.getheight(); // ARGB_4444 Bitmap returnbmp = Bitmap.createBitmap((int) dw, (int) dh,bitmap.config.argb_4444); try { //. BitmapFactory.Options bmpfactoryoptions = new BitmapFactory.Options(); bmpfactoryoptions.injustdecodebounds = true; returnbmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); int heightratio = (int) Math.ceil(bmpFactoryOptions.outHeight / dh); int widthratio = (int) Math.ceil(bmpFactoryOptions.outWidth / dw); Log.v( HEIGHTRATIO, + heightratio); Log.v( WIDTHRATIO, + widthratio); // 1. if (heightratio > 1 && widthratio > 1) { if (heightratio > widthratio) { //. bmpfactoryoptions.insamplesize = heightratio;
103 else { //. bmpfactoryoptions.insamplesize = widthratio; //. bmpfactoryoptions.injustdecodebounds = false; returnbmp = BitmapFactory.decodeStream(getContentResolver(). ë openinputstream(imagefileuri), null, bmpfactoryoptions); catch (FileNotFoundException e) { Log.v( ERROR, e.tostring()); return returnbmp; XML <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_parent android:layout_height= fill_parent > <Button android:layout_width= fill_parent android:layout_height= wrap_content android:id= @+id/choosepicturebutton1 android:text= Choose Picture 1 /> <Button android:layout_width= fill_parent android:layout_height= wrap_content android:id= @+id/choosepicturebutton2 android:text= Choose Picture 2 /> <ImageView android:layout_width= wrap_content android:layout_height= ë wrap_content android:id= @+id/compositeimageview ></ImageView> </LinearLayout> transfermode 3 17 3 22
104 CHAPTER 3
105
106 CHAPTER 3
107 API