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. Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”

    今天老板让我验证一下技术可行性,记录下来. 需求 :定位手机的位置并在百度地图上显示,得到位置后使用前置摄像头进行抓拍 拿到这个需求后,对于摄像头的使用不太熟悉,于是我先做了定位手机并在百度地图上显示 ...

  2. 蓝桥杯之K好数

    如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22.30.3 ...

  3. C#获得时间段

    DateTime today = dt.Date;//今天 00:00:00 DateTime tomorrow = dt.Date.AddDays(1);//明天 00:00:00 DateTime ...

  4. 前端面试题系列(1):doctype作用 标准模式与兼容模式

    1.doctype作用 <!DOCTYPE>声明位于HTML文档的第一行.处于<HTML>标签之前.告知浏览器的解析器用什么文档标准解析这个文档.DOCYTYPE不存在或格式不 ...

  5. Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)

    来源自:http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Most network-connected ...

  6. laravel中with()方法,has()方法和whereHas()方法的区别

    with() with()方法是用作"渴求式加载"的,那主要意味着,laravel将会伴随着主要模型预加载出确切的的关联关系.这就对那些如果你想加在一个模型的所有关联关系非常有帮助 ...

  7. A:点排序-poj

    A:点排序 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个点的坐标(x, y),在输入的n个点中,依次计算这些点到指定点的距离,并按照距离进行从小到大排序,并且输出点的坐标 ...

  8. Android studio导出配置

    在使用 Android Studio 时,往往会进行一些设置,比如 界面风格.字体.字体大小.快捷键.常用模板等.但是这里的设置只能用在一个版本的 Android Studio 上,如果下载了新的 A ...

  9. 在O(n)时间复杂度内找到出现超过一半的数

    #include<iostream> using namespace std; bool solver(const int a[],const int n, int & num) ...

  10. caffe+opencv3.3dnn模块 完成手写数字图片识别

    最近由于项目需要用到caffe,学习了下caffe的用法,在使用过程中也是遇到了些问题,通过上网搜索和问老师的方法解决了,在此记录下过程,方便以后查看,也希望能为和我一样的新手们提供帮助. 顺带附上老 ...