如何用Java对图片进行马赛克处理?

把图片压缩成32*32这个不难,java有这样的类提供了方法BufferedImage bi = new BufferedImage(w * 2 / scale, h * 2 / scale, bm.getType());//画布大小Graphics2D g2 = bi.createGraphics();g2.drawImage(bm, 0, 0, w * 2 / scale, h * 2 / scale, null); //绘制缩小后的图// 转换成JPEG图像格式JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);JPEGEncodeParam jpeg = encoder.getDefaultJPEGEncodeParam(bi);jpeg.setQuality(1f, false);encoder.setJPEGEncodeParam(jpeg);encoder.encode(bi);byte[] data = bos.toByteArray();但是要减少颜色数的话,这个比较麻烦,这个首先需要知道图片中颜色空间,比如16色,32色等,你需要读取图片文件中的内容,然后根据图片头的信息读取每个像素的RGB值(如果是RGB空间),并对每一个像素进行 颜色 设置

java适合做图像处理吗?

Java图像处理技巧四则

下面代码中用到的sourceImage是一个已经存在的Image对象

对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:

//importjava.awt.*;

//importjava.awt.image.*;

ImagecroppedImage;

ImageFiltercropFilter;

CropFilter=newCropImageFilter(25,30,75,75);//四个参数分别为图像起点坐标和宽高,即CropImageFilter(intx,inty,intwidth,intheight),详细情况请参考API

CroppedImage=Toolkit.getDefaultToolkit().createImage(newFilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。

对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:

ImagescaledImage=sourceImage.getScaledInstance(100,100,Image.SCALE_DEFAULT);//得到一个100X100的图像

ImagedoubledImage=sourceImage.getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2,Image.SCALE_DEFAULT);//得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。

//其它情况请参考API

下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用R*0.3+G*0.59+B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。

/*GrayFilter.java*/

/*@author:cherami*/

/*email:cherami@163.net*/

importjava.awt.image.*;

publicclassGrayFilterextendsRGBImageFilter{

intmodelStyle;

publicGrayFilter(){

modelStyle=GrayModel.CS_MAX;

canFilterIndexColorModel=true;

publicGrayFilter(intstyle){

modelStyle=style;

canFilterIndexColorModel=true;

publicvoidsetColorModel(ColorModelcm){

if(modelStyle==GrayModel

elseif(modelStyle==GrayModel

publicintfilterRGB(intx,inty,intpixel){

returnpixel;

/*GrayModel.java*/

/*@author:cherami*/

/*email:cherami@163.net*/

importjava.awt.image.*;

publicclassGrayModelextendsColorModel{

publicstaticfinalintCS_MAX=0;

publicstaticfinalintCS_FLOAT=1;

ColorModelsourceModel;

intmodelStyle;

publicGrayModel(ColorModelsourceModel){

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=0;

publicGrayModel(ColorModelsourceModel,intstyle){

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=style;

publicvoidsetGrayStyle(intstyle){

modelStyle=style;

protectedintgetGrayLevel(intpixel){

if(modelStyle==CS_MAX){

returnMath.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));

elseif(modelStyle==CS_FLOAT){

return(int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);

else{

return0;

publicintgetAlpha(intpixel){

returnsourceModel.getAlpha(pixel);

publicintgetRed(intpixel){

returngetGrayLevel(pixel);

publicintgetGreen(intpixel){

returngetGrayLevel(pixel);

publicintgetBlue(intpixel){

returngetGrayLevel(pixel);

publicintgetRGB(intpixel){

intgray=getGrayLevel(pixel);

return(getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;

如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。

根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:

/*ReverseColorModel.java*/

/*@author:cherami*/

/*email:cherami@163.net*/

importjava.awt.image.*;

publicclassReverseColorModelextendsColorModel{

ColorModelsourceModel;

publicReverseColorModel(ColorModelsourceModel){

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

publicintgetAlpha(intpixel){

returnsourceModel.getAlpha(pixel);

publicintgetRed(intpixel){

return~sourceModel.getRed(pixel);

publicintgetGreen(intpixel){

return~sourceModel.getGreen(pixel);

publicintgetBlue(intpixel){

return~sourceModel.getBlue(pixel);

publicintgetRGB(intpixel){

return(getAlpha(pixel)<<24)+(getRed(pixel)<<16)+(getGreen(pixel)<<8)+getBlue(pixel);

/*ReverseColorModel.java*/

/*@author:cherami*/

/*email:cherami@163.net*/

importjava.awt.image.*;

publicclassReverseFilterextendsRGBImageFilter{

publicReverseFilter(){

canFilterIndexColorModel=true;

publicvoidsetColorModel(ColorModelcm){

substituteColorModel(cm,newReverseColorModel(cm));

publicintfilterRGB(intx,inty,intpixel){

returnpixel;

要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。

下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/

/*@author:cherami*/

/*email:cherami@163.net*/

importjava.awt.*;

importjava.awt.image.*;

importjavax.swing.*;

importjava.awt.color.*;

publicclassGrayImageextendsJFrame{

Imagesource,gray,gray3,clip,bigimg;

BufferedImagebimg,gray2;

GrayFilterfilter,filter2;

ImageIconii;

ImageFiltercropFilter;

intiw,ih;

publicGrayImage(){

ii=newImageIcon(\"images/11.gif\");

source=ii.getImage();

iw=source.getWidth(this);

ih=source.getHeight(this);

filter=newGrayFilter();

filter2=newGrayFilter(GrayModel.CS_FLOAT);

gray=createImage(newFilteredImageSource(source.getSource(),filter));

gray3=createImage(newFilteredImageSource(source.getSource(),filter2));

cropFilter=newCropImageFilter(5,5,iw-5,ih-5);

clip=createImage(newFilteredImageSource(source.getSource(),cropFilter));

bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);

MediaTrackermt=newMediaTracker(this);

mt.addImage(gray,0);

mt.waitForAll();

}catch(Exceptione){

publicvoidpaint(Graphicsg){

Graphics2Dg2=(Graphics2D)g;

bimg=newBufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB);

Graphics2DsrcG=bimg.createGraphics();

RenderingHintsrhs=g2.getRenderingHints();

srcG.setRenderingHints(rhs);

srcG.drawImage(source,0,0,null);

ColorSpacegraySpace=ColorSpace.getInstance(ColorSpace.CS_GRAY);

ColorConvertOpop=newColorConvertOp(graySpace,rhs);

gray2=newBufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB);

op.filter(bimg,gray2);

g2.drawImage(source,40,40,this);

g2.drawImage(gray,80,40,this);

g2.drawImage(gray2,120,40,this);

g2.drawImage(gray3,160,40,this);

g2.drawImage(clip,40,80,this);

g2.drawImage(bigimg,80,80,this);

publicvoidupdate(Graphicsg){

paint(g);

publicstaticvoidmain(Stringargs[]){

GrayImagem=newGrayImage();

m.setSize(400,400);

m.setVisible(true);

参考资料:

开发者在线

JAVA实现将上传的图片缩放处理是什么?

imageio。ImageIO;import java。awt。geom。AffineTransform;public class UploadImg{/*** @param fromdir 图片的原始目录* @param todir 处理后的图片存放目录* @param imgfile 原始图片* @param sysimgfile 处理后的图片文件名前缀**/……public boolean CreateThumbnail() throws Exception{//ext是图片的格式 gif JPG 或pngString ext="";double Ratio=0。

JAVA技术如何上传图片的缩放处理呢?

0;File F = new File(fromdir,imgfile);if (!F。isFile())throw new Exception(F+" is not image file error in CreateThumbnail!");//首先判断上传的图片是gif还是JPG ImageIO只能将gif转换为pngif (isJpg(imgfile)){ext="jpg";}else{ext="png"; }File ThF = new File(todir,sysimgfile+"。

如何用Java对图片进行马赛克处理相关推荐

  1. java实现马赛克,java如何用Processing生成马赛克风格的图像

    java如何用Processing生成马赛克风格的图像 首先使用PImage来实例化对象,再通过loadImage赋值,两层for循环遍历图片上的像素点,每隔5个像素点,画一个直径为3的圆.颜色通过p ...

  2. java 图片 rgb_简单的java图片处理——如何用Java读出一张图片的RGB值?

    如何用Java读出一张图片的RGB值? (牛人可以从第六步开始看,牛人看了代码就知道怎么建文件夹啦!! ) (1) 在刚刚建立项目的根目录新建一个文件夹date,例如,我的这个项目的根目录是:E:\W ...

  3. 余数定理_如何用Java实现余数定理

    余数定理 by Anuj Pahade 由Anuj Pahade 如何用Java实现余数定理 (How to implement the Chinese Remainder Theorem in Ja ...

  4. preparestatement方法用多次_如何用java 5分钟实现一个最简单的mysql代理服务器?

    用java8基于vert.x3 快速实现一个最简单的mysql代理服务器,只需要5分钟时间. 什么是mysql 代理? mysql代理是介于client端和mysql服务端中间层服务,如下图所示: 这 ...

  5. 如何用Java创建不可变的Map

    你好朋友, 在本教程中,我们将看到如何用Java创建不可变的Map. –不可变的类或对象是什么意思? –什么是不可变地图? –如何在Java中创建不可变的Map? 不变的类或对象是什么意思? 不可变的 ...

  6. 如何用Java代码解析json

    如何用Java代码解析json 今天在写项目的时候用到了json,然后尝试着Java代码解析了一下json 用相同的类型定义一个参数,然后json.与定义的参数类型一样的get方法,括号里面就是要解析 ...

  7. java 模拟登陆 post_Java开发网 - 高手帮忙啊 (如何用java模拟post方式进行登陆论坛?)...

    于 2003-05-03 02:51 请教高手 我如何用java模拟post方式进行登陆论坛? 我先 URLConnection uc=htpurl.openConnection(); uc.setD ...

  8. 如何用命令行写java程序_如何用java实现doc命令行

    如何用java实现doc命令行, dir显示当前所有目录下的文件 cd 文件目录 进入到该目录 cd ..退到上级目录,一面是我写的代码,调试了. 不过有点小bug就是当你进去的是文件就会提示空指针异 ...

  9. 初始化 数组 java_如何用Java初始化数组

    初始化 数组 java Today we will learn how to initialize an array in java. An array in java is a container ...

最新文章

  1. MATLAB【六】 ———— matlab 随机散斑模拟
  2. SpringCloud服务组合
  3. hadoop 2.2.0 终于编译ok了
  4. Ubuntu中给eclipse和android studio添加桌面快捷图标
  5. Axure RP 8.0正式版下载地址 安装和汉化说明
  6. 001. Ansible简介
  7. [转载] StringBuffer和StringBuilder类
  8. c#参数修饰符-params
  9. python之文件读写
  10. I00028 整数逆序
  11. 虚拟串口服务器怎manager,VSPManager虚拟串口管理软件
  12. msf介绍及其常用模块
  13. scrapy_redis分布式爬虫遇到的问题DEBUG: Filtered offsite request to
  14. Beyong Compare 4 提示 “这个授权已被吊销” 的解决办法
  15. 几个创业小故事的启示
  16. uni-app使用ucharts地图(主要微信小程序-初步使用)
  17. Winfrom窗体应用程序图标的改变
  18. LLJ-300HS;LLJ-F(S)系列漏电继电器
  19. 交通大学计算机科学考研_选择计算机科学作为大学专业之前需要知道的事情
  20. 如何写出优秀的单元测试

热门文章

  1. MySQL的基本语法(四)
  2. 大牛首聚DTCC:分享数据治理应用案例
  3. 怎么样才能让程序以 高性能NVIDIA图形处理器运行
  4. graphpad prism横坐标怎么设置不显示数值_Graphpad Prism能不能画九象限图?
  5. tfidf和word2vec构建文本词向量并做文本聚类
  6. 混沌现代[置顶] 科学探索需要炼金术士精神
  7. 国产耳机哪个牌子品牌音质好?音质最好的国产蓝牙耳机推荐
  8. 软件优化理论基础以及方法论小结.
  9. python plt 画动态折线图
  10. p图软件pⅰc_‎App Store 上的“相机360 - 潮流P图新玩法”