图片处理工具类

1人阅读 评论(0) 收藏 举报
package com.imooc.myapplication;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * Created by Administrator on 2014/12/20 0020.
 */
public class ImageHelper {

    public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue);
        hueMatrix.setRotate(1, hue);
        hueMatrix.setRotate(2, hue);

        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);

        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bm, 0, 0, paint);

        return bmp;
    }

    public static Bitmap handleImageNegative(Bitmap bm) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        int color;
        int r, g, b, a;

        Bitmap bmp = Bitmap.createBitmap(width, height
                , Bitmap.Config.ARGB_8888);

        int[] oldPx = new int[width * height];
        int[] newPx = new int[width * height];
        bm.getPixels(oldPx, 0, width, 0, 0, width, height);

        for (int i = 0; i < width * height; i++) {
            color = oldPx[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            if (r > 255) {
                r = 255;
            } else if (r < 0) {
                r = 0;
            }
            if (g > 255) {
                g = 255;
            } else if (g < 0) {
                g = 0;
            }
            if (b > 255) {
                b = 255;
            } else if (b < 0) {
                b = 0;
            }
            newPx[i] = Color.argb(a, r, g, b);
        }
        bmp.setPixels(newPx, 0, width, 0, 0, width, height);
        return bmp;
    }

    public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) {
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
                Bitmap.Config.ARGB_8888);
        int width = bm.getWidth();
        int height = bm.getHeight();
        int color = 0;
        int r, g, b, a, r1, g1, b1;

        int[] oldPx = new int[width * height];
        int[] newPx = new int[width * height];

        bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
        for (int i = 0; i < width * height; i++) {
            color = oldPx[i];
            a = Color.alpha(color);
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);

            r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
            g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
            b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);

            if (r1 > 255) {
                r1 = 255;
            }
            if (g1 > 255) {
                g1 = 255;
            }
            if (b1 > 255) {
                b1 = 255;
            }

            newPx[i] = Color.argb(a, r1, g1, b1);
        }
        bmp.setPixels(newPx, 0, width, 0, 0, width, height);
        return bmp;
    }

    public static Bitmap handleImagePixelsRelief(Bitmap bm) {
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
                Bitmap.Config.ARGB_8888);
        int width = bm.getWidth();
        int height = bm.getHeight();
        int color = 0, colorBefore = 0;
        int a, r, g, b;
        int r1, g1, b1;

        int[] oldPx = new int[width * height];
        int[] newPx = new int[width * height];

        bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
        for (int i = 1; i < width * height; i++) {
            colorBefore = oldPx[i - 1];
            a = Color.alpha(colorBefore);
            r = Color.red(colorBefore);
            g = Color.green(colorBefore);
            b = Color.blue(colorBefore);

            color = oldPx[i];
            r1 = Color.red(color);
            g1 = Color.green(color);
            b1 = Color.blue(color);

            r = (r - r1 + 127);
            g = (g - g1 + 127);
            b = (b - b1 + 127);
            if (r > 255) {
                r = 255;
            }
            if (g > 255) {
                g = 255;
            }
            if (b > 255) {
                b = 255;
            }
            newPx[i] = Color.argb(a, r, g, b);
        }
        bmp.setPixels(newPx, 0, width, 0, 0, width, height);
        return bmp;
    }
}


查看评论

java图片处理工具类,很实用哦

少壮不努力,老大徒伤悲。好好学习,天天向上!
  • lou7441822
  • lou7441822
  • 2017-08-21 17:24:43
  • 884

java图片处理工具类

  • 2015年01月25日 12:33
  • 20KB
  • 下载

Android 图片处理工具类汇总

很有用的Android图片处理工具,实现各种图片处理效果
  • shell812
  • shell812
  • 2015-11-11 14:27:44
  • 1660

Java的图片处理工具类ImageUtils

可实现以下常用功能:缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等...
  • yulei2008_
  • yulei2008_
  • 2016-07-19 10:11:10
  • 15689

Java图片处理 - 创建工具类

Java图片处理 - 创建工具类
  • chwshuang
  • chwshuang
  • 2017-03-22 19:39:32
  • 1915

Swing图片处理工具类ImageHelper

package zju.archeo.images; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image...
  • cs064
  • cs064
  • 2013-10-24 22:36:51
  • 1192

Java图片处理工具类库推荐---ThumbNailtor

前一阵子,做公司OA系统开发时,需要上传个人图片作为个人头像,但是每个人上传的图片像素大小不统一,所以需要对图片进行处理,偶然发现了一个广泛使用的类库。 Thumbnailator 是一个为Java...
  • XueZhuoYiWang
  • XueZhuoYiWang
  • 2016-07-29 22:44:55
  • 2284

C#开源工具类(字符串处理加密、邮件、图片处理 数据库连接等)

  • 2010年03月31日 22:18
  • 124KB
  • 下载

超详细的图片处理 工具类

声明: 本文是方便自己在以后的项目里使用。 使用场景(图片处理 转格式 拿图片 ) package com.example.testdemo; import java.io.ByteAr...
  • hao_m582
  • hao_m582
  • 2016-12-21 22:37:52
  • 220

Android实用工具类-GrallyAndPhotoUtils图片处理工具

图片处理工具,加载缩略图,旋转图片,打开相册及拍照
  • u011374875
  • u011374875
  • 2016-04-15 16:58:10
  • 5680
    个人资料
    等级:
    访问量: 3485
    积分: 305
    排名: 24万+
    文章分类