网站首页 > 文章中心 > 其它

java实现裁剪算法代码

作者:小编 更新时间:2023-08-23 19:29:45 浏览量:96人看过

JAVA实现整数拆分算法,例如输入一个4会输出4 , 3 1, 2 2, 2 1

import?java.util.Scanner;?//输入的

public?class?ABS?{??//外面建的点?java的文件名必须和这个一样

static?void?p(int?n,?int?index)?//搜索

{

int?i;

java实现裁剪算法代码-图1

if(n?=?0)?????//当n为0的时候输出这种情况

System.out.print(a[0]);

for(i?=?1;?i?index;?System.out.print("?+?"?+?a[i++]));

System.out.print("\n");

return;????//返回到函数调用的地方

}

for(i?=?index?0?(n?=?a[index?-?1])?a[index?-?1]?:?n;?i?0;?i--)

java实现裁剪算法代码-图2

{?//如果?数组下标大于0且?n剩余的值大于等于上一个的值,那么i就等于上一个的值,?否则i就等于n

a[index]?=?i;

p(n?-?i,?index?+?1);?//在次调用

public?static?void?main(String[]?args)?{?

Scanner?in?=?new?Scanner(System.in);?//从控制台中读取输入数据

int?t;

t?=?in.nextInt();???//输入一个整数

if(t?=?10000)

System.out.println("你输入的数据超过1万,?太大咯!!!");

return?;

p(t,0);?

in.close();?//?关闭输入

java实现裁剪算法代码-图3

这个只是可以实现10000 以内的数, 如果想大一点 就把数组开大一点局可以了!

您好!请问用java怎么将截取png的图片中间一部分,以及如何压缩一个png图片?

getSubimage方法是进行图片裁剪.

举例:

public static void main(String[] args) {

try {

//从特定文件载入

BufferedImage bi = ImageIO.read(new File("c:\\test.png"));

bi.getSubimage(0, 0, 10, 10);//前两个值是坐标位置X、Y,后两个是长和宽

} catch (IOException e) {

e.printStackTrace();

以下是进行的图片压缩,涉及到多个工具类.

/**

* 图片工具类

* 压缩图片大小

* @author Cyw

* @version 1.0

*/

public class ZIPImage {

private File file = null;

private String outPutFilePath;

private String inPutFilePath;

private String inPutFileName;

private boolean autoBuildFileName;

private String outPutFileName;

private int outPutFileWidth = 100; // 默认输出图片宽

private int outPutFileHeight = 100; // 默认输出图片高

private static boolean isScaleZoom = true; // 是否按比例缩放

public ZIPImage() {

outPutFilePath = "";

inPutFilePath = "";

inPutFileName = "";

autoBuildFileName = true;

outPutFileName = "";

*?

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

outPutFileName = opfn;

* @param aBFN

* 是否自动生成目标文件名

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,

boolean aBFN) {

autoBuildFileName = aBFN;

public boolean isAutoBuildFileName() {

return autoBuildFileName;

public void setAutoBuildFileName(boolean autoBuildFileName) {

this.autoBuildFileName = autoBuildFileName;

public String getInPutFilePath() {

return inPutFilePath;

public void setInPutFilePath(String inPutFilePath) {

this.inPutFilePath = inPutFilePath;

public String getOutPutFileName() {

return outPutFileName;

public void setOutPutFileName(String outPutFileName) {

this.outPutFileName = outPutFileName;

public String getOutPutFilePath() {

return outPutFilePath;

public void setOutPutFilePath(String outPutFilePath) {

this.outPutFilePath = outPutFilePath;

public int getOutPutFileHeight() {

return outPutFileHeight;

public void setOutPutFileHeight(int outPutFileHeight) {

this.outPutFileHeight = outPutFileHeight;

public int getOutPutFileWidth() {

return outPutFileWidth;

public void setOutPutFileWidth(int outPutFileWidth) {

this.outPutFileWidth = outPutFileWidth;

public boolean isScaleZoom() {

return isScaleZoom;

public void setScaleZoom(boolean isScaleZoom) {

this.isScaleZoom = isScaleZoom;

public String getInPutFileName() {

return inPutFileName;

public void setInPutFileName(String inPutFileName) {

this.inPutFileName = inPutFileName;

* @return boolean

public boolean compressImage() {

boolean flag = false;

if (inPutFilePath.trim().equals("")) {

throw new NullPointerException("源文件夹路径不存在.");

if (inPutFileName.trim().equals("")) {

throw new NullPointerException("图片文件路径不存在.");

if (outPutFilePath.trim().equals("")) {

throw new NullPointerException("目标文件夹路径地址为空.");

} else {

if (!ZIPImage.mddir(outPutFilePath)) {

throw new FileNotFoundException(outPutFilePath

+ " 文件夹创建失败!");

if (this.autoBuildFileName) { // 自动生成文件名

String tempFile[] = getFileNameAndExtName(inPutFileName);

outPutFileName = tempFile[0] + "_cyw." + tempFile[1];

compressPic();

if (outPutFileName.trim().equals("")) {

throw new NullPointerException("目标文件名为空.");

} catch (Exception e) {

flag = false;

return flag;

// 图片处理

private void compressPic() throws Exception {

// 获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ " 文件不存在!");

Image img = ImageIO.read(file);

// 判断图片格式是否正确

if (img.getWidth(null) == -1) {

throw new Exception("文件不可读!");

int newWidth;

int newHeight;

// 判断是否是等比缩放

if (ZIPImage.isScaleZoom == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) img.getWidth(null))

/ (double) outPutFileWidth + 0.1;

/ (double) outPutFileHeight + 0.1;

// 根据缩放比率大的进行缩放控制

newWidth = (int) (((double) img.getWidth(null)) / rate);

newHeight = (int) (((double) img.getHeight(null)) / rate);

newWidth = outPutFileWidth; // 输出的图片宽度

newHeight = outPutFileHeight; // 输出的图片高度

BufferedImage tag = new BufferedImage((int) newWidth,

(int) newHeight, BufferedImage.TYPE_INT_RGB);

/*

* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

tag.getGraphics().drawImage(

img.getScaledInstance(newWidth, newHeight,

Image.SCALE_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath

+ outPutFileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

} catch (IOException ex) {

ex.printStackTrace();

* 创建文件夹目录

* @param filePath

* @return

* @throws Exception

@SuppressWarnings("unused")

private static boolean mddir(String filePath) throws Exception {

File f = new File(filePath);

if (!f.exists()) {

flag = f.mkdirs();

flag = true;

* 获得文件名和扩展名

* @param fullFileName

private String[] getFileNameAndExtName(String fullFileName)

throws Exception {

if (fullFileName.indexOf(".") == -1) {

throw new Exception("源文件名不正确!");

fileNames[0] = fullFileName.substring(0, fullFileName

.lastIndexOf("."));

fileNames[1] = fullFileName

.substring(fullFileName.lastIndexOf(".") + 1);

return fileNames;

public Image getSourceImage() throws IOException{

//获得源文件

return img;

* 获得图片大小?

* @path :图片路径

public long getPicSize(String path) {

File file = new File(path);

return file.length();

//下面是测试程序

package com.sun.util.cyw;

import java.awt.Image;

import java.io.IOException;

public class ImageTest {

public static void main(String[] args) throws IOException {

ZIPImage zip=new ZIPImage("d:\\","1.jpg","d:\\test\\","处理后的图片.jpg",false);

zip.setOutPutFileWidth(1000);

zip.setOutPutFileHeight(1000);

Image image=zip.getSourceImage();

long size=zip.getPicSize("d:\\1.jpg");

System.out.println("处理前的图片大小 width:"+image.getWidth(null));

System.out.println("处理前的图片大小 height:"+image.getHeight(null));

System.out.println("处理前的图片容量:"+ size +" bit");

zip.compressImage();

我有一个签名图片如何用java代码裁剪掉空白区域

用imageIO 读取进入BufferedImage,检测图片非空白大小,建立新的BufferedImage,拷贝非空白区至新的BufferedImage, 用imageIO 再次保存.

求个用JAVA或者C++写的AO*算法的程序

还有下面的

C++中AO裁剪关键代码

ITopologicalOperatorPtr ipTopolog(CLSID_Line);

long lCount = 0;

while (SUCCEEDED(ipSorCursor-NextFeature(ipFeature))

(NULL != ipFeature)) {

result = ipFeature-get_Shape(ipGeometry);

if (FAILED(result) || NULL == ipGeometry) {

TRACE("%ld\n", result);

continue;

((IZAwarePtr)ipGeometry)-put_ZAware(VARIANT_FALSE);

((IMAwarePtr)ipGeometry)-put_MAware(VARIANT_FALSE);

// 入库图形都需要裁剪才能入库的.

ipTopolog = ipGeometry;

result = ipTopolog-Intersect((IGeometryPtr)ipGridPolygon, esriGeometry1Dimension, ipResult);

if (FAILED(result) || NULL == ipResult) {

result = ipResult-get_IsEmpty(vb);

if (VARIANT_TRUE == vb) {

...

希望可以帮到你

以上就是土嘎嘎小编为大家整理的java实现裁剪算法代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章