導航:首頁 > 編程語言 > java拼接圖片

java拼接圖片

發布時間:2023-01-13 21:48:51

java實現多個tif文件圖片拼接

publicstaticvoidmany2one(List<String>bookFilePaths,StringtoPath,StringdistFileName){
if(bookFilePaths!=null&&bookFilePaths.size()>0){
File[]files=newFile[bookFilePaths.size()];
for(inti=0;i<bookFilePaths.size();i++){
files[i]=newFile(bookFilePaths.get(i));
}
if(files!=null&&files.length>0){

try{
ArrayListpages=newArrayList(files.length-1);
FileSeekableStream[]stream=newFileSeekableStream[files.length];
for(inti=0;i<files.length;i++){
stream[i]=newFileSeekableStream(
files[i].getCanonicalPath());
}
ParameterBlockpb=(newParameterBlock());
PlanarImagefirstPage=JAI.create("stream",stream[0]);
for(inti=1;i<files.length;i++){
PlanarImagepage=JAI.create("stream",stream[i]);
pages.add(page);

}
TIFFEncodeParamparam=newTIFFEncodeParam();
Filef=newFile(toPath);
if(!f.exists()){
f.mkdirs();
}
OutputStreamos=newFileOutputStream(toPath+File.separator+distFileName);
ImageEncoderenc=ImageCodec.createImageEncoder("tiff",
os,param);
param.setExtraImages(pages.iterator());
enc.encode(firstPage);
for(inti=0;i<files.length;i++){
stream[i].close();
if(files[i].isFile()&&files[i].exists()){
files[i].delete();
}
}
os.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}

❷ java中Image獲得相對路徑的圖片,求代碼

Image img=Image.getInstance("images/image-left.jpg");
這樣就可以了

❸ java用相對路徑將圖片上傳到WebContent下的images文件夾

  1. 獲取上下文

    從request中取,得到webapp路徑。

    String dir = request.getServletContext().getRealPath("/") ;

  2. 拼接文件夾

    String saveDir = dir + "images";


❹ java如何將多張圖片連接成一張圖片

Image bgImage;
Image fgImage;
try {
bgImage = ImageIO.read(bgImageFile);
fgImage = ImageIO.read(fgImageFile);
int bgWidth = bgImage.getWidth(null);
int bgHeight = bgImage.getHeight(null);
int fgWidth = fgImage.getWidth(null);
int fgHeight = fgImage.getHeight(null);
// 此類叫SpliceImages,這個方法是靜態方法,所以實例化一下
// 後面的這個對象的屬性是自定義的類成員。
SpliceImages spliceImages = new SpliceImages();
// 私有方法
spliceImages.parsePosition(position, bgWidth, bgHeight, fgWidth, fgHeight);
// 生成新圖片的長、寬,後面那個是色彩模式
BufferedImage bufferedImage = new BufferedImage(spliceImages.WIDTH, spliceImages.HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.createGraphics();
// 繪制背景圖片
graphics.drawImage(bgImage, spliceImages.bgX, spliceImages.bgY, null);
// 繪制前景圖片
graphics.drawImage(fgImage, spliceImages.fgX, spliceImages.fgY, null);
graphics.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(bufferedImage);
output.close();
} catch (IOException e) {
e.printStackTrace();
}

這個是我做圖表時寫的,整個類中定義了很多重載方法,所以不全貼了。由於用的FusionCharts3.1,單系列圖表沒有圖例,為了處理導出圖片,就自製圖例,然後用java拼接。其中那個私有方法是我處理兩張圖片的左上角坐標的。這里就不貼了。實際上我的做法就是取出兩個圖片,然後重新繪制。做法比較笨,希望拋磚引玉吧。同理,圖片覆蓋也是用這個,只是兩圖片的坐標發生變發。

❺ Java 圖片拼接

圖像無縮放?
這個就是個演算法的問題吧,你就直接根據多少行多少列,挨個給像素點賦值啊。不過要求原始圖像大小相同吧。

❻ 我在java實訓裡面遇到圖片無法顯示的問題,事情是這樣的。我們用的是java EE當我們把項目導入

圖片路徑拼接不全,從控制台看,你的路徑只有一部分。。。

❼ 求教貼,Java桌面小游戲的地圖怎麼做

小游戲地圖一般都是各種圖片的拼接,然後保存到2維數組裡面

比如

//數值常量
publicstaticfinalintEMPTY=0;//空地什麼也沒有
publicstaticfinalintBRICK=1;//土牆
publicstaticfinalintSTONE=2;//石頭
publicstaticfinalintSEA=3;//海洋
publicstaticfinalintGREENS=4;//草地
publicstaticfinalintSNOW=5;//雪地
//對應的圖片
.....
publicstaticfinalImageIMG_STONE=newImageIcon(MapValues.class.getResource("/resource/imgs/stone.png")).getImage();//石頭圖片
......

那麼保存一副地圖就可以用類似於下面的二維數組表示

002405100
145111123
132131001

使用的時候, 載入二維數組,然後把對應的圖片顯示在地圖上就可以了.

不過這樣做也存在一定的問題.

1:圖片太多,對應的數字太多, 那麼地圖編輯起來很麻煩, 需要有強大的記憶力,和想像力

2:不能一邊編輯,一邊就顯示出效果來, 出錯後,排查麻煩. 效率太低

所以,我們還需要寫點代碼,做一個簡單的地圖編輯器,添加輔助線,擦除工具等, 這樣的話, 編輯地圖就非常方便了,所見即所得.並且一鍵可以保存為地圖數據(二維數組)

學習swing的時候,寫過一個簡單的地圖編輯器,效果圖如下

❽ 怎麼樣用Java實現將一張圖片轉成字元畫

#首先在D盤寫一個文件"temp.html",如下內容

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>圖片轉文本</title>
<meta http-equiv="content-type" content="text/html; charset=gbk">
<style type="text/css">
body {
font-family: 宋體; line-height: 0.8em; letter-spacing: 0px; font-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 = 0; x < bfedimage.getWidth(); x++) {
for (int y = 0; y < 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 = 0; y < css[0].length; y++) {
for (int x = 0; x < css.length; x++) {
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"文件看效果吧。

有什麼問題可以聯系我。

❾ java 發送郵件裡面想嵌套一張圖片,發出去怎麼弄

拼接前端頁面,添加css樣式:增加背景圖片樣式!

閱讀全文

與java拼接圖片相關的資料

熱點內容
軟考初級程序員大題分值 瀏覽:471
js壓縮視頻文件 瀏覽:578
linux如何通過命令創建文件 瀏覽:989
應用加密app還能訪問應用嘛 瀏覽:432
安卓怎麼用支付寶交違章罰款 瀏覽:665
php面向對象的程序設計 瀏覽:504
數據挖掘演算法書籍推薦 瀏覽:894
投訴聯通用什麼app 瀏覽:150
web伺服器變更ip地址 瀏覽:954
java正則表達式驗證郵箱 瀏覽:360
成熟商務男裝下載什麼軟體app 瀏覽:609
加密2h代表長度是多少厘米 瀏覽:23
拍賣程序員 瀏覽:101
電腦的圖片放在哪個文件夾 瀏覽:274
unsignedintjava 瀏覽:216
編譯器下載地址 瀏覽:42
什麼是面對對象編程 瀏覽:708
b站伺服器什麼時候恢復 瀏覽:721
6p相當於安卓機什麼水準 瀏覽:499
能否給隱藏相冊加密 瀏覽:598