19 Android中图片处理原理篇,关于人脸识别网站,图片加载到内存,图片缩放,图片翻转倒置,网上撕衣服游戏案例编写

               

1 加载图片到内存

1.数码相机照片特别是大于3m以上的,内存吃不消,会报OutOfMemoryError,若是想只显示原图片的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);

 
 

3 获取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");

 
 

4 gallery获取一个图片

 

Intent  intent = new Intent(Intent.ACTION_PICK);

 

intent.setType(“image/*”);

 

intent.getData()  获取imageuri

 

Bitmap  bmp = BitmapFactory.decodeStream(getContentResolver().

 

openInputStream(imageFileUri),  null, bmpFactoryOptions);

 
 

5 创建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);

 
 

6 图形缩放

 

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

 
 

7 图形旋转

 

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);

 
 

8 图像平移:

 

setTranslate(1.5f,-10);

 

9 镜子效果:

 

matrix.setScale(-1,  1);

 

matrix.postTranslate(bmp.getWidth(),0);

 

10 倒影效果:

 

matrix.setScale(1,  -1);

 

matrix.postTranslate(0,  bmp.getHeight());

 
 

11 图像颜色处理:

 

颜色矩阵  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));

 
 

12 变换图像的亮度

 

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();

 

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));

 
 

13 更改图片的饱和度:

 

ColorMatrix  cm = new ColorMatrix();

 

cm.setSaturation(.5f);

 

paint.setColorFilter(new  ColorMatrixColorFilter(cm));

 
 

14 图像合成:

 

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);

 
 

15 按指定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);

 
 

16 人脸识别

 

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]);     

 

         //调用函数对人脸画面进行处理

 

     }

 

}

 
 

关于人脸识别部分(网站地址是):

 

http://www.faceplusplus.com/

 

 

============================================================================

1  场景:一张图片很大,放到手机上时需要对图片资源进行压缩以及缩放,编写如下界面的案例:

2 操作:当点击加载图片到内存时,图片从自己的手机sd卡中取到并显示。

3 ADT开发时,手机连接上电脑后,在Android开发工具中的”FileExplorer”中的文件位置如下:

4 下面开始编写代码,项目结构如下:

5 编写activity_main.xml,代码如下:

 
 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 

     xmlns:tools="http://schemas.android.com/tools"

 

     android:layout_width="match_parent"

 

     android:layout_height="match_parent"

 

     android:orientation="vertical"

 

     tools:context=".MainActivity"  >

 

 

 

    <Button

 

        android:onClick="click"

 

        android:layout_width="fill_parent"

 

        android:layout_height="wrap_content"

 

        android:text="加载图片到内存" />

 

 

 

    <ImageView

 

         android:id="@+id/iv"

 

        android:layout_width="fill_parent"

 

        android:layout_height="fill_parent"  />

 

 

 

</LinearLayout>

 

6 编写MainActivity,代码如下:

 
 

package com.itheima.loadimg;

 

 

 

import android.app.Activity;

 

import android.graphics.Bitmap;

 

import android.graphics.BitmapFactory;

 

import android.graphics.BitmapFactory.Options;

 

import android.os.Bundle;

 

import android.view.View;

 

import android.view.WindowManager;

 

猜你喜欢

转载自blog.csdn.net/qq_44884269/article/details/89359966