`
dlcoco9999
  • 浏览: 38026 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

java九宫格切割缩放并合并

    博客分类:
  • java
阅读更多
//缩放
public void zoomImage(File out,String name,int rtype,int cw,int ch,int w,int h) throws Exception
{
BufferedImage bimg0 = ImageIO.read(new File(out, name));
int bw=bimg0.getWidth();
int bh=bimg0.getHeight();
int type = -1;
String format = null;
if (name.toLowerCase().endsWith("png")) {
format = "PNG";
type = BufferedImage.TYPE_INT_ARGB;

} else {
format = "JPG";
type = BufferedImage.TYPE_INT_RGB;
}
int zw=0;
int zh=0;
switch(rtype)
{
case 1:
//宽度ZOOM
zw=w-2*cw;
zh=ch;
break;
case 2:
//高度ZOOM
zw=cw;
zh=h-2*ch;
break;
case 3:
//高宽ZOOM
zw=w-2*cw;
zh=h-2*ch;
break;
}
BufferedImage image=new BufferedImage(zw,zh,type);
Graphics g = image.getGraphics();
g.drawImage(bimg0,0, 0,zw,zh, 0, 0, bw, bh, null);
ImageIO.write(image, format, new File(out, name));
}
//合并9图
public void mergeImage(File out,String name,String[] images,int w,int h) throws Exception
{
BufferedImage bimg0 = ImageIO.read(new File(out.getParentFile(), name));
int type = -1;
String format = null;
if (name.toLowerCase().endsWith("png")) {
format = "PNG";
type = BufferedImage.TYPE_INT_ARGB;

} else {
format = "JPG";
type = BufferedImage.TYPE_INT_RGB;
}

BufferedImage image=new BufferedImage(w,h,type);
Graphics g = image.getGraphics();

int x=0;
int y=0;
for(int i=0;i<images.length;i++)
{
BufferedImage bimg = ImageIO.read(new File(out, images[i]));
g.drawImage(bimg,x,y,bimg.getWidth(),bimg.getHeight(),null);
if(i%3==0)
x=x+bimg.getWidth();
else if(i%3==1)
x=x+bimg.getWidth();
else if(i%3==2)
x=0;
//g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer)


if(i%3==2)
y=y+bimg.getHeight();


}
ImageIO.write(image, format, new File(out.getParentFile(), name));
}
/**
* 九宫格切割处理图片
*/
private void cut9Image(File out,String name,int w,int h) throws Exception
{
File cache=new File(out.getPath()+"/cache");
if(!cache.exists())
cache.mkdir();
//原图
BufferedImage bimg0 = ImageIO.read(new File(out, name));
String[] images=new String[9];
List list=new ArrayList();

int w0=bimg0.getWidth();
int h0=bimg0.getHeight();
int type = -1;
String format = null;
if (name.toLowerCase().endsWith("png")) {
format = "PNG";
type = BufferedImage.TYPE_INT_ARGB;

} else {
format = "JPG";
type = BufferedImage.TYPE_INT_RGB;
}
//切割成9部分
/**
* 边角四块的高度和宽度
*/
int cornerwidth=3;
int cornerheight=3;
//左上
BufferedImage image1=new BufferedImage(cornerwidth,cornerheight,type);
//右上
BufferedImage image2=new BufferedImage(cornerwidth,cornerheight,type);
//左下
BufferedImage image3=new BufferedImage(cornerwidth,cornerheight,type);
//右下
BufferedImage image4=new BufferedImage(cornerwidth,cornerheight,type);

int topwidth=w0-2*cornerwidth;
int topheight=cornerheight;
//TOP
BufferedImage topbimg=new BufferedImage(topwidth,topheight,type);
int leftwidth=cornerwidth;
int leftheight=h0-2*cornerheight;
//LEFT
BufferedImage leftbimg=new BufferedImage(leftwidth,leftheight,type);
int midwidth=w0-2*cornerwidth;
int midheight=h0-2*cornerheight;
//middle
BufferedImage midbimg=new BufferedImage(midwidth,midheight,type);
int rightwidth=cornerwidth;
int rightheight=h0-2*cornerheight;
//right
BufferedImage rightbimg=new BufferedImage(rightwidth,rightheight,type);
int bottomwidth=w0-2*cornerwidth;
int bottomheight=cornerheight;
//bottom
BufferedImage bottombimg=new BufferedImage(bottomwidth,bottomheight,type);

Graphics g1 = image1.getGraphics();
Graphics g2 = image2.getGraphics();
Graphics g3 = image3.getGraphics();
Graphics g4 = image4.getGraphics();
Graphics gtop = topbimg.getGraphics();
Graphics gleft = leftbimg.getGraphics();
Graphics gmid = midbimg.getGraphics();
Graphics gright = rightbimg.getGraphics();
Graphics gbottom = bottombimg.getGraphics();
String cutname="";
//左上
g1.drawImage(bimg0, 0, 0,cornerwidth,cornerheight, 0, 0, cornerwidth, cornerheight, null);
cutname="image1."+format;
ImageIO.write(image1, format, new File(cache, cutname));
images[0]=cutname;
list.add(image1);
//top
gtop.drawImage(bimg0,0, 0, topwidth, topheight, cornerwidth, 0, cornerwidth+topwidth,topheight, null);
cutname="top."+format;
ImageIO.write(topbimg, format, new File(cache, cutname));
zoomImage(cache,cutname,1,cornerwidth,cornerheight,w,h);
images[1]=cutname;
list.add(topbimg);
//右上
g2.drawImage(bimg0,0, 0, cornerwidth, cornerheight,cornerwidth+topwidth, 0, 2*cornerwidth+topwidth,cornerheight,  null);
cutname="image2."+format;
ImageIO.write(image2, format, new File(cache, cutname));
images[2]=cutname;
list.add(image2);
//左
gleft.drawImage(bimg0,0, 0, leftwidth, leftheight, 0, cornerheight, 0 + leftwidth, cornerheight+leftheight,  null);
cutname="left."+format;
ImageIO.write(leftbimg, format, new File(cache, cutname));
zoomImage(cache,cutname,2,cornerwidth,cornerheight,w,h);
images[3]=cutname;
list.add(leftbimg);
//中
gmid.drawImage(bimg0,0, 0, midwidth, midheight, cornerwidth, cornerheight, cornerwidth+midwidth, cornerheight+midheight,  null);
cutname="mid."+format;
ImageIO.write(midbimg, format, new File(cache,cutname));
zoomImage(cache,cutname,3,cornerwidth,cornerheight,w,h);
images[4]=cutname;
list.add(midbimg);
//右
gright.drawImage(bimg0,0, 0, rightwidth, rightheight, cornerwidth+midwidth, cornerheight,2*cornerwidth+midwidth, cornerheight+midheight,  null);
cutname="right."+format;
ImageIO.write(rightbimg, format, new File(cache, cutname));
zoomImage(cache,cutname,2,cornerwidth,cornerheight,w,h);
images[5]=cutname;
list.add(rightbimg);
//左下
g3.drawImage(bimg0, 0, 0, cornerwidth, cornerheight,0, cornerheight+leftheight, 0 + cornerwidth, 2*cornerheight+leftheight,  null);
cutname="image3."+format;
ImageIO.write(image3, format, new File(cache,cutname));
images[6]=cutname;
list.add(image3);
//底
gbottom.drawImage(bimg0,0, 0, bottomwidth, bottomheight, cornerwidth, cornerheight+midheight, cornerwidth+bottomwidth, 2*cornerheight+midheight,  null);
cutname="bottom."+format;
ImageIO.write(bottombimg, format, new File(cache,cutname));
zoomImage(cache,cutname,1,cornerwidth,cornerheight,w,h);
images[7]=cutname;
list.add(bottombimg);
//右下
g4.drawImage(bimg0,0, 0, cornerwidth, cornerheight, cornerwidth+bottomwidth, cornerheight+rightheight, 2*cornerwidth+bottomwidth, 2*cornerheight+rightheight,  null);
cutname="image4."+format;
ImageIO.write(image4, format, new File(cache,cutname));
images[8]=cutname;
list.add(image4);

mergeImage(cache,name,images,w,h);

for(int i=0;i<images.length;i++)
{
File file=new File(cache,images[i]);
if(file.exists())
file.delete();
}
cache.deleteOnExit();

}
分享到:
评论

相关推荐

    微信群聊生成九宫格形式的头像

    至于提供的源码,"JavaApk源码说明.txt"可能是对源代码的简单说明,"下载更多打包源码~.url"可能是指向更多相关源码的链接,而"防微信群聊九宫格头像"可能是包含具体实现的Java源文件,可以从中学习到上述技术的具体...

    PHP多张图片合并成一张图片合并成九宫格图片.zip

    在PHP开发中,有时我们需要将多张图片合并成一张,比如创建九宫格图片,用于社交媒体分享、个性化头像或动态壁纸等。本教程将详细讲解如何使用PHP实现这一功能,以及如何通过提供的代码示例生成类似的效果。 首先,...

    Windows -九宫格图片切割器

    1. 首先,下载并解压名为“九宫格图片切割器”的压缩文件,运行其中的可执行程序。 2. 打开软件后,界面简洁明了。点击“选择图片”按钮,从本地电脑中选取你想要切割的原始图片。 3. 图片加载完成后,软件会自动...

    Android-Android图片选择预览九宫格图片控件拖拽排序九宫格图片控件

    本文将详细讲解如何实现一个Android图片选择、预览功能,并且介绍如何设计一个支持拖拽排序的九宫格图片控件。 首先,让我们关注“Android图片选择”这一知识点。在Android中,我们可以利用系统的`Intent`来调用...

    自定义九宫格图片布局

    最后,为了使九宫格布局美观,我们还需要考虑图片的缩放和填充方式。通常,我们可以使用`android:scaleType="centerCrop"`来保持图片的宽高比,并填充视图区域。 ```xml android:id="@+id/image_view" android:...

    Android 图片选择、预览、九宫格图片控件、拖拽排序九宫格图片控件

    在Android应用开发中,处理图片是一项常见的任务,无论是让用户选择图片、预览图片,还是在九宫格中展示和拖拽排序图片。本教程将详细讲解如何实现这些功能,主要聚焦于一个开源库——BGAPhotoPicker-Android。这个...

    九宫格背景图,实现复杂可拉伸背景图。

    值得注意的是,虽然九宫格背景图可以提供较好的自适应效果,但对图像的处理要求较高,需要设计师精心策划并切割图片,以达到理想的视觉效果。此外,对于现代浏览器的支持情况良好,但在一些旧版本或者非主流浏览器中...

    仿朋友圈,新浪微博九宫格图片缩放

    在Android应用开发中,"仿朋友圈,新浪微博九宫格图片缩放"是一个常见的需求,它涉及到用户界面(UI)设计和图像处理技术。这个功能可以让用户在浏览类似微信朋友圈或新浪微博这样的社交网络时,轻松查看并交互图片...

    AS3.0九宫格

    3. **应用九宫格**:利用AS3.0提供的`BitmapData`和`Rectangle`类,可以精确地控制图像的切割与拼接。代码示例中的`BitmapScale9Grid`类就是一个具体的实现,它接受源图像和九宫格参数,然后通过`getBitmap`函数分割...

    Flex位图九宫格处理工具

    这个类可能包含了一些方法,如设置九宫格切割位置,以及在缩放时如何正确地绘制位图的部分。 2. `BitmapRepeatUtil.as`:另一个ActionScript类,可能是用来处理位图的重复部分,如边缘和中间区域。这个工具类可能...

    ActionScript3位图九宫格缩放工具类

    帮助flash内实现图片的9宫格缩放,位图九宫格缩放,ActionScript3代码实现。

    winform窗体动画九宫格视野演示

    描述中的"支持任意图片的九宫格逻辑"意味着该程序能够接受任何图像文件作为输入,并将其自动适应九宫格的显示模式。这涉及到图像处理技术,包括图片的裁剪、缩放和排列。开发者可能使用了C#中的Graphics类或者第三方...

    Android自定义九宫格抽奖控件

    "Android自定义九宫格抽奖控件"就是一个很好的实例,它解决了在实际业务中对抽奖界面的特殊需求,允许开发者灵活控制抽奖转盘的旋转次数、速度以及最终停留的位置。这种控件不仅能够提供更丰富的交互体验,还能适应...

    图片控件九宫格互换

    4. **图像处理**:在图片互换的过程中,可能需要对图像进行裁剪、缩放等操作,以适应九宫格内各单元格的大小。 5. **用户交互反馈**:为了提供良好的用户体验,还需要添加适当的动画效果,如拖动时的图像跟随,以及...

    九宫格的用法

    在IT行业中,"九宫格"是一个常见的设计概念,尤其在UI设计和图形用户界面中。九宫格的用法主要是为了实现界面元素的自适应布局和视觉平衡,尤其是...通过理解并熟练运用九宫格,可以提升数字产品的整体质量和用户体验。

    KLSD.rar_KLSD_android_android 九宫格_九宫格_九宫格26984

    在Android中,我们可以使用Animation类或者Transition API来添加各种动画,如平移、旋转、缩放等,让九宫格在用户交互时展现出动态美感。 压缩包中的"KLSD"文件可能是整个项目的源代码,包含了Java或Kotlin的类文件...

    ios-简单九宫格移动效果.zip

    在iOS开发中,实现“简单九宫格移动效果”通常涉及到使用UIKit框架,特别是UIView及其相关的动画API。这个效果可能是创建一个包含九个单元格的网格布局,每个单元格可以独立移动,形成一种动态视觉效果。以下是实现...

    H5抽奖九宫格页面

    1. **规划页面结构**:确定页面的元素,如背景、九宫格、按钮、提示信息等,并规划它们在页面上的位置和交互逻辑。 2. **HTML结构**:使用HTML5标签构建基本的页面结构,每个九宫格可以是一个`&lt;div&gt;`元素,包含奖品...

    android 九宫格 GridView

    为了使九宫格更美观,还可以添加各种动画效果,如渐变显示、缩放等。同时,为了提高用户体验,可以考虑添加长按事件、滑动删除等功能。此外,对于性能优化,还可以考虑使用DiffUtil来减少不必要的视图更新。 总的来...

Global site tag (gtag.js) - Google Analytics