1、Load图形到内存
1.数码相机照片特别大3m以上,内存吃不消,只显示原图的1/8
通过BitmapFactory.Options 来实现
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inSampleSize = 8;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
imv.setImageBitmap(bmp);
2.根据当前屏幕分辨率的大小,加载图片
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight();
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
Log.v("HEIGHTRATIO",""+heightRatio);
Log.v("WIDTHRATIO",""+widthRatio);
/判断是否要进行缩放
if (heightRatio > 1 && widthRatio > 1)
{
if (heightRatio > widthRatio)
{
//高度变化大,按高度缩放
bmpFactoryOptions.inSampleSize = heightRatio;
}
else
{
// 宽度变化大,按宽度缩放
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
示例代码:
package com.itheima.loadimage;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private int windowWidth ;
private int windowHeight ;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
//1. 获取手机屏幕的分辨率 宽高
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
windowHeight = display.getHeight();
System.out.println("窗体的宽度:"+windowWidth);
System.out.println("窗体的高度:"+windowHeight);
}
public void click(View view){
Options opts = new Options();
//设置解析器 不去真正的解析这个位图 而是解析这个图片的out输出信息,(宽度 高度) 不会为图片的每个点 申请内存空间
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile("/sdcard/IMG_0017.JPG", opts);
int picHeight = opts.outHeight;
int picWidth = opts.outWidth;
System.out.println("图片的宽度:"+picWidth);
System.out.println("图片的高度:"+picHeight);
int scaleX = picWidth / windowWidth;
int scaleY = picHeight / windowHeight;
System.out.println("水平缩放比例:"+scaleX);
System.out.println("垂直缩放比例:"+scaleY);
int scale = 1;
if(scaleX>scaleY && scaleY >1){
//按照水平缩放
scale = scaleX;
}
if(scaleY>scaleX && scaleX >1){
//按照竖直平缩放
scale = scaleY;
}
//真正的解析这个位图
opts.inJustDecodeBounds = false;
opts.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/IMG_0017.JPG",opts);
iv.setImageBitmap(bitmap);
}
}
2、获取Exif图片信息
//从文件获取exif信息
ExifInterface ei = new ExifInterface(imageFilePath);
String imageDescription = ei.getAttribute("ImageDescription");
if (imageDescription != null)
{
Log.v("EXIF", imageDescription);
}
//把exif信息写到文件:
ExifInterface ei = new ExifInterface(imageFilePath);
ei.setAttribute("ImageDescription","Something New");
示例代码:
package com.example.exif;
import java.io.IOException;
import android.media.ExifInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
ExifInterface exif = new ExifInterface("/sdcard/IMG_0017.JPG");
System.out.println(exif.getAttribute(ExifInterface.TAG_DATETIME));
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、从gallery获取一个图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(“image/*”);
intent.getData() 获取image的uri
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().�
openInputStream(imageFileUri), null, bmpFactoryOptions);
示例代码:
package com.itheima.selectimage;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
public void click(View view){
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data!=null){
Uri uri = data.getData();
try {
InputStream is = getContentResolver().openInputStream(uri);
Options opts = new Options();
opts.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), opts);
iv.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
4、创建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);
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
//创建图形的拷贝
canvas.drawBitmap(bitmap, new Matrix(), paint);
paint.setColor(Color.BLACK);
canvas.drawText("美女", 10, 10, paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
5、图形缩放
Matrix matrix = new Matrix();
matrix.setValues(new float[] {
1, 0, 0,
0, 1, 0,
0, 0, 1
});
x = 1x + 0y + 0z
y = 0x + 1y + 0z
z = 0x + 0y + 1z
通过canvas.drawBitmap(bmp, matrix, paint);创建bitmap
1.水平缩放0.5
2.垂直拉扯2倍
matrix.setScale(1.5f,1);//水平点放大到1.5f,垂直1
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth()/2, (int)(bitmap.getHeight()*1.2), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
Matrix matrix = new Matrix();
// matrix.setValues(new float[] {
// 0.5f, 0, 0,
// 0, 2, 0,
// 0, 0, 1
// });
matrix.setScale(0.5f, 1.2f);
//创建图形的拷贝
canvas.drawBitmap(bitmap, matrix, paint);
paint.setColor(Color.BLACK);
canvas.drawText("美女", 10, 10, paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
6、图形旋转
Matrix matrix = new Matrix();
matrix.setRotate(15);
canvas.drawBitmap(bmp, matrix, paint);
消除锯齿
paint.setAntiAlias(true);
指定圆心的旋转
matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);
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);
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth()+60, bitmap.getHeight(), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
Matrix matrix = new Matrix();
matrix.setRotate(30, bitmap.getWidth()/2, bitmap.getHeight()/2);
//创建图形的拷贝
canvas.drawBitmap(bitmap, matrix, paint);
paint.setColor(Color.BLACK);
canvas.drawText("美女", 10, 10, paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
7、图形平移
setTranslate(1.5f,-10);
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth()+60, bitmap.getHeight(), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
Matrix matrix = new Matrix();
matrix.postTranslate(20, 0);
//创建图形的拷贝
canvas.drawBitmap(bitmap, matrix, paint);
paint.setColor(Color.BLACK);
canvas.drawText("美女", 10, 10, paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
8、镜子效果
matrix.setScale(-1, 1);
matrix.postTranslate(bmp.getWidth(),0);
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
Matrix matrix = new Matrix();
matrix.setScale(-1, 1);
matrix.postTranslate(bitmap.getWidth(),0);
//创建图形的拷贝
canvas.drawBitmap(bitmap, matrix, paint);
paint.setColor(Color.BLACK);
canvas.drawText("美女", 10, 10, paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
9、倒影效果
matrix.setScale(1, -1);
matrix.postTranslate(0, bmp.getHeight());
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
10、图像颜色处理
颜色矩阵 ColorMatrix cm = new ColorMatrix();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 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
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));
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
private SeekBar seekBar1;
private Canvas canvas ;
private Paint paint;
private Bitmap bitmap;
private Bitmap alterBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress(); // 0~100
float r = progress*100.0f/100;
paint = new Paint();
Matrix matrix = new Matrix();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
r, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
//创建图形的拷贝
canvas.drawBitmap(bitmap, matrix, paint);
iv_dest.setImageBitmap(alterBitmap);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
canvas = new Canvas(alterBitmap);
paint = new Paint();
paint.setAntiAlias(true);
Matrix matrix = new Matrix();
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));
//创建图形的拷贝
canvas.drawBitmap(bitmap, matrix, paint);
paint.setColor(Color.BLACK);
canvas.drawText("美女", 10, 10, paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
11、变换图像的亮度
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));
12、更改图片饱和度
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(.5f);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
13、图片合成
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.�
PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(bmp2, 0, 0, paint);
示例代码:
package com.itheima.getimagecopy;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_dest = (ImageView) findViewById(R.id.iv_dest);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
iv_src.setImageURI(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
//创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
//原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(is);
//创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
//下面的代码创建的图片 是一个空白的图片
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
//创建图形的拷贝
canvas.drawBitmap(bitmap, new Matrix(), paint);
paint.setColor(Color.BLACK);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Matrix(), paint);
iv_dest.setImageBitmap(alterBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
14、按指定path上绘制文字
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setTextSize(20);
paint.setTypeface(Typeface.DEFAULT);
Path p = new Path();
p.moveTo(20, 20);
p.lineTo(100, 150);
p.lineTo(200, 220);
canvas.drawTextOnPath("Hello this is text on a path", p, 0, 0, paint);
15、人脸识别
FaceDetector detector = new FaceDetector(faceBitmap.getWidth(),
faceBitmap.getHeight(), 3); // 创建识别器
mNumFaces = detector.findFaces(faceBitmap, mFaces);
// 识别
if (mNumFaces > 0) {
for (int i = 0; i < mNumFaces; i++) {
handleFace(mFaces[i]);
// 调用函数对人脸画面进行处理
}
}
PointF midPoint = new PointF();
int r = ((int) (f.eyesDistance() * mScale * 1.5)); // 取眼睛间距离
f.getMidPoint(midPoint); // 取脸的中点
midPoint.x *= mScale;
midPoint.y *= mScale;
Canvas c = new Canvas(mBitmap);
Paint p = new Paint();
p.setAntiAlias(true);
p.setAlpha(0x80);
c.drawCircle(midPoint.x, midPoint.y, r, p); // 用半透明标出人脸区域;
mImageView.setImageBitmap(mBitmap); // 显示图片
示例代码:
package com.example.findface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aaa);
FaceDetector detector = new FaceDetector(bitmap.getWidth(),
bitmap.getHeight(), 3); // 创建识别器
Face[] faces = new Face[3];
int mNumFaces = detector.findFaces(bitmap, faces);
System.out.println("找到脸的个数:"+mNumFaces);
// 识别
if (mNumFaces > 0) {
for (int i = 0; i < mNumFaces; i++) {
System.out.println("找到一张脸");
}
}
}
}
16、美女扒衣服
package com.itheima.girl;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
// 原图的一个拷贝
private Bitmap alterBitmap;
private Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pre);
// 得到一个空白的大小跟原图一样的 可以修改的bitmap
alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, new Matrix(), paint);
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x;
int y;
switch (event.getAction()) {
// 手指按下
case MotionEvent.ACTION_DOWN:
System.out.println("摸到!");
x = (int) event.getX();
y = (int) event.getY();
for (int i = 3; i > -2; i--) {
for (int j = 3; j > -2; j--) {
alterBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);
}
}
iv.setImageBitmap(alterBitmap);
break;
// 手指移动
case MotionEvent.ACTION_MOVE:
System.out.println("移动");
x = (int) event.getX();
y = (int) event.getY();
for (int i = 3; i > -2; i--) {
for (int j = 3; j > -2; j--) {
alterBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);
}
}
iv.setImageBitmap(alterBitmap);
break;
// 手指离开屏幕的一瞬间
case MotionEvent.ACTION_UP:
System.out.println("松手..");
break;
}
return true;
}
});
}
}