java实现多个tif文件图片拼接

Python016

java实现多个tif文件图片拼接,第1张

public static void many2one(List<String> bookFilePaths, String toPath,String distFileName) {

    if (bookFilePaths != null && bookFilePaths.size() > 0) {

     File[] files = new File[bookFilePaths.size()]

     for(int i = 0 i < bookFilePaths.size() i++){

      files[i] =  new File(bookFilePaths.get(i))

     }

     if (files != null && files.length > 0) {

      

      try {

       ArrayList pages = new ArrayList(files.length - 1)

       FileSeekableStream[] stream = new FileSeekableStream[files.length]

       for (int i = 0 i < files.length i++) {

        stream[i] = new FileSeekableStream(

          files[i].getCanonicalPath())

       }

       ParameterBlock pb = (new ParameterBlock())

       PlanarImage firstPage = JAI.create("stream", stream[0])

       for (int i = 1 i < files.length i++) {

        PlanarImage page = JAI.create("stream", stream[i])

        pages.add(page)

  

       }

       TIFFEncodeParam param = new TIFFEncodeParam()

       File f = new File(toPath)

       if(!f.exists()){

        f.mkdirs()

       }

       OutputStream os = new FileOutputStream(toPath + File.separator+ distFileName)

       ImageEncoder enc = ImageCodec.createImageEncoder("tiff",

         os, param)

       param.setExtraImages(pages.iterator())

       enc.encode(firstPage)

       for (int i = 0 i < files.length i++) {

        stream[i].close()

           if(files[i].isFile()&&files[i].exists()){

         files[i].delete()

        }

       }

       os.close()

      } catch (IOException e) {

       e.printStackTrace()

      }

     }

    }

   }

#首先在D盘写一个文件"temp.html",如下内容

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>图片转文本</title>

<meta http-equiv="content-type" content="text/htmlcharset=gbk">

<style type="text/css">

body {

font-family: 宋体line-height: 0.8emletter-spacing: 0pxfont-size: 8px

}

</style>

</head>

<body>

${content}

</body>

</html>

#在D盘放一个图片(放小一点的)"a.jpg"

#运行如下JAVA代码:

import java.awt.Color

import java.awt.image.BufferedImage

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStreamReader

import javax.imageio.ImageIO

public class Test {

/** 此处设置灰度字符,此处只用十个字符,可以设置更多 */

private static char[] cs = new char[] { '.', ',', '*', '+', '=', '&', '$', '@', '#', ' ' }

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

// 读取图片

BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"))

// 图片转字符串后的数组

char[][] css = new char[bfedimage.getWidth()][bfedimage.getHeight()]

for (int x = 0x <bfedimage.getWidth()x++) {

for (int y = 0y <bfedimage.getHeight()y++) {

int rgb = bfedimage.getRGB(x, y)

Color c = new Color(rgb)

// 得到灰度值

int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3

css[x][y] = cs[(int) ((cc * 10 - 1) / 255)]

}

}

// 取得模板HTML

String temp = readFile(new File("D:\\temp.html"),"gbk")

StringBuffer sb = new StringBuffer()

// 开始拼接内容

for (int y = 0y <css[0].lengthy++) {

for (int x = 0x <css.lengthx++) {

sb.append(css[x][y])

}

sb.append("\r\n")

}

System.out.println(sb.toString())

// 生成文件

String content = toHTML(sb.toString())

String filecontent = replaceStrAllNotBack(temp, "${content}", content)

writeFile(new File("D:\\content.html"), filecontent, "gbk")

}

public static String toHTML(String s) {

s = s.replaceAll("&", "&")

s = s.replaceAll(" ", " ")

s = s.replaceAll(">", ">")

s = s.replaceAll("<", "<")

s = s.replaceAll("\"", """)

s = s.replaceAll("\\\r\\\n", "<br/>")

s = s.replaceAll("\\\r", "<br/>")

s = s.replaceAll("\\\n", "<br/>")

return s

}

public static String replaceStrAllNotBack(String str, String strSrc, String strDes) {

StringBuffer sb = new StringBuffer(str)

int index = 0

while ((index = sb.indexOf(strSrc, index)) != -1) {

sb.replace(index, index + strSrc.length(), strDes)

index += strDes.length()

}

return sb.toString()

}

/**

* 读文件(使用默认编码)

*

* @param file

* @return 文件内容

* @throws IOException

*/

public static String readFile(File file, String charset) throws IOException {

InputStreamReader fr = new InputStreamReader(new FileInputStream(file), charset)

StringBuffer sb = new StringBuffer()

char[] bs = new char[1024]

int i = 0

while ((i = fr.read(bs)) != -1) {

sb.append(bs, 0, i)

}

fr.close()

return sb.toString()

}

/**

* 写文件

*

* @param file

* @param string

*字符串

* @param encoding

*编码

* @return 文件大小

* @throws IOException

*/

public static int writeFile(File file, String string, String encoding) throws IOException {

FileOutputStream fos = new FileOutputStream(file)

try {

byte[] bs = string.getBytes(encoding)

fos.write(bs)

return bs.length

} finally {

fos.close()

}

}

}

#打开"D:\content.html"文件看效果吧。

有什么问题可以联系我。