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() 获取image的uri

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”中的文件位置例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

4 以下開始编写代码,项目结构例如以下:

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

<LinearLayout )、手指在一张美女图片上移动时。移动部分的图片会变成成透明。然后显示底部的另外一张图片

(2)、当手指离开的时候播放音乐…

应用效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

1 编写应用,代码结构例如以下:

2、编写布局文件activity_main.xml

<RelativeLayout 、编写MainActivity

package com.itheima.play;

import android.app.Activity;

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.media.MediaPlayer;

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

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

// 创建一个空白的原图的拷贝

alertBitmap = Bitmap.createBitmap(bitmap.getWidth(),

bitmap.getHeight(), bitmap.getConfig());

canvas = new Canvas(alertBitmap);

Paint paint = new Paint();

paint.setColor(Color.BLACK);

canvas.drawBitmap(bitmap, new Matrix(), paint);

iv.setImageBitmap(alertBitmap);

iv.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:// 手指按下屏幕

System.out.println("action down");

break;

case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动

int x = (int) event.getX();

int y = (int) event.getY();

System.out.println("设置("+x+","+y+")透明颜色");

for(int i=-4;i<5;i++){

for(int j=-4;j<5;j++){

try{

alertBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);

} catch (Exception e) {

e.printStackTrace();

}

}

}

iv.setImageBitmap(alertBitmap);

break;

case MotionEvent.ACTION_UP:// 手指离开屏幕

MediaPlayer.create(getApplicationContext(), R.raw.higirl).start();

break;

}

return true;//能够反复循环的处理事件

}

});

}

}

4  Android的清单文件例如以下:

<?xml version="1.0" encoding="utf-8"?

>

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

package="com.itheima.play"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="19" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.itheima.play.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>



19_Android中图片处理原理篇,关于人脸识别站点,图片载入到内存,图片缩放,图片翻转倒置,网上撕衣服游戏案例编写的更多相关文章

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

    1加载图片到内存 (1).数码相机照片特别是大于3m以上的,内存吃不消,会报OutOfMemoryError,若是想只显示原图片的1/8,可以通过BitmapFactory.Options来实现,具体 ...

  2. Android人脸识别Demo竖屏YUV方向调整和图片保存

    本博客包含三个常用方法,用于盛开Android版人脸识别Demo中竖屏使用时送入yuv数据,但一直无法识别的情况. 1.首先可以尝试顺时针旋转90°或270°,然后送入识别SDK. 2.旋转方向后依然 ...

  3. 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】

    文章来自于:http://blog.renren.com/share/246648717/8171467499 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴 ...

  4. html5与EmguCV前后端实现——人脸识别篇(一)

    上个月因为出差的关系,断更了很久,为了补偿大家长久的等待,送上一个新的系列,之前几个系列也会抽空继续更新. 大概半年多前吧,因为工作需要,我开始研究图像识别技术.OpenCV在这方面已经有了很多技术积 ...

  5. opencv基于PCA降维算法的人脸识别

    opencv基于PCA降维算法的人脸识别(att_faces) 一.数据提取与处理 # 导入所需模块 import matplotlib.pyplot as plt import numpy as n ...

  6. AI大厂算法测试心得:人脸识别关键指标有哪些?

    仅仅在几年前,程序员要开发一款人脸识别应用,就必须精通算法的编写.但现在,随着成熟算法的对外开放,越来越多开发者只需专注于开发垂直行业的产品即可. 由调查机构发布的<中国AI产业地图研究> ...

  7. 总结几个简单好用的Python人脸识别算法

    原文连接:https://mp.weixin.qq.com/s/3BgDld9hILPLCIlyysZs6Q 哈喽,大家好. 今天给大家总结几个简单.好用的人脸识别算法. 人脸识别是计算机视觉中比较常 ...

  8. C#实现基于ffmepg加虹软的人脸识别

    关于人脸识别 目前的人脸识别已经相对成熟,有各种收费免费的商业方案和开源方案,其中OpenCV很早就支持了人脸识别,在我选择人脸识别开发库时,也横向对比了三种库,包括在线识别的百度.开源的OpenCV ...

  9. C#实现基于ffmpeg加虹软的人脸识别demo及开发分享

    对开发库的C#封装,屏蔽使用细节,可以快速安全的调用人脸识别相关API.具体见github地址.新增对.NET Core的支持,在Linux(Ubuntu下)测试通过.具体的使用例子和Demo详解,参 ...

随机推荐

  1. 图片格式 WebP APNG

    WebP  是一种支持有损压缩和无损压缩的图片文件格式,派生自图像编码格式 VP8.根据 Google 的测试,无损压缩后的 WebP 比 PNG 文件少了 45% 的文件大小,即使这些 PNG 文件 ...

  2. 到底vuex是什么?

    Vuex 什么是Vuex? 官方说法:Vuex 是一个专为 Vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 个人理 ...

  3. Python中time和datetime模块的简单用法

    python中与时间相关的一个模块是time模块,datetime模块可以看为是time模块的高级封装. time模块中经常用到的有一下几个方法: time()用来获取时间戳,表示的结果为从1970年 ...

  4. NFC (Near Filed Communication)

    NFC的用途:近场通信(Near Field Communication,NFC),又称近距离无线通信,是一种短距离的高频无线通信技术,允许电子设备之间进行非接触式点对点数据传输(在十厘米内)交换数据 ...

  5. python+selenium安装

    1.下载Python 请到官网自行下载安装https://www.python.org/downloads/ 在安装的时候,注意一定要勾上这个选项,可以免去我们配置系统变量的麻烦,如果你忘了,没关系, ...

  6. 70、django之Ajax初识

    Ajax准么说是用于Javascript与服务器端进行交互的,我们之前呢没有了解ajax也同样可以完成与服务器的交互,那么ajax的优势在哪里?首先ajax是异步交互的也就是说我们基本不会遇到卡顿现象 ...

  7. oracle精简客户端安装配置及常见问题

    有关Instant client 安装步骤 1.首先在官网下载两个安装包instant/sqlplus,对相关文件进行解压缩,存放本地路径 官网地址:http://www.oracle.com/tec ...

  8. IpHelper根据客户端IP进行网站分流

    public class IpHelper    {        // 核心方法:IP搜索       /// <summary>        /// 查找IP所属地区,确保web.c ...

  9. c#DES加密解密代码

    //加密  public string DesEncrypt(string strText, string strEncrKey)   {    byte[] byKey=null;    byte[ ...

  10. Fis3迁移至Webpack实战

    Webpack从2015年9月第一个版本横空初始至今已逾2载.它的出现,颠覆了一大批主流构建如Ant.Grunt和Gulp等等.腾讯NOW直播IVWEB团队之前一直采用Fis构建,本篇文章主要介绍从F ...