導航:首頁 > 編程語言 > java圖片疊加

java圖片疊加

發布時間:2024-12-08 01:43:49

『壹』 java 如何將多張JPG圖片合成視頻文件,比如:avi格式 或 mpg格式.

之前有做過圖片合成視頻的功能,大概代碼就是這樣,你可以看一下
/**
* 圖片合成視頻
* @param mp4SavePath 視頻保存路徑
* @param imageDir 圖片地址
* @param rate 這個可以理解成視頻每秒播放圖片的數量
*/
public static boolean jpgToMp4(String mp4SavePath, String imageDir, double rate) {
FFmpegFrameRecorder recorder = null;
boolean flag = true;
try {
File[] files = FileUtils.fileSort(imageDir);
int [] widthArray = new int[files.length];
int [] heightArray = new int[files.length];

/**
* 獲取合成視頻圖片的最大寬高,避免圖片比例不一致最終合成效果差
*/
for (int i = 0; i < files.length; i++) {
BufferedImage bufferedImage = ImageIO.read(files[i]);
widthArray[i] = bufferedImage.getWidth();
heightArray[i] = bufferedImage.getHeight();
}

/**
* 這個方法主要是防止圖片比例達不到視頻合成比例的要求,如果達不到下面條件視頻則會無法播放
* 圖片寬:必須要被32整除
* 圖片高:必須要被2整除
*/
int [] maxWH = getImgMaxWH(widthArray,heightArray);
recorder = new FFmpegFrameRecorder(mp4SavePath,maxWH[0],maxWH[1]);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
/**
* 視頻質量:目前測試出來的是25-30最清晰,視頻質量范圍好像是0-40,具體可以自己慢慢測
*/
recorder.setVideoQuality(25);
recorder.setFormat("mp4");
recorder.setFrameRate(rate > 0 ? rate : 1);
recorder.setPixelFormat(0);
recorder.start();

OpenCVFrameConverter.ToIplImage conveter = new OpenCVFrameConverter.ToIplImage();

/**
* 合成視頻
*/
for(int i = 0; i < files.length; i++ ){
opencv_core.IplImage image = cvLoadImage(files[i].getPath());
recorder.record(conveter.convert(image));
opencv_core.cvReleaseImage(image);
}
logger.info("合成成功");
} catch(Exception e) {
e.printStackTrace();
flag = false;
logger.error("合成失敗");
} finally {
try {
if (recorder != null){
recorder.stop();
recorder.release();
}
} catch (FrameRecorder.Exception e) {
e.printStackTrace();
}
}
return flag;
}

『貳』 java中怎樣在panel中同時設置幾張圖片(並且可以重疊)

自已實現一個類,該類繼承自JPanel類,然後復寫裡面的paintComponent(Graphics g)方法,在這個方法裡面作如下操作:

Graphics2D gfx = (Graphics2D) g;

gfx.drawImage(img1, ...);

gfx.drawImage(img2, ...);


  1. 關於drawImage(...)如何使用,可以參見Java API文檔,該介面有好幾個變種,你可以選擇適合自己要求的哪一個;

  2. 另外,你還可以設置圖片之間的疊加效果,使用Java2D裡面的AlphaComposite,然後再drawImage(...)之前調用gfx.setPaint(AlphaComposite paint)即可;


希望上述提示可以幫到你,謝謝~

『叄』 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圖片疊加相關的資料

熱點內容
程序員自帶電腦 瀏覽:123
android怎麼學 瀏覽:426
pdf怎麼蓋電子章 瀏覽:413
在哪裡可以租個雲伺服器 瀏覽:861
基於msp430單片機的設計 瀏覽:559
比較好用的編程軟體 瀏覽:160
php抓取動態網頁 瀏覽:185
c51單片機原 瀏覽:196
交行app在哪裡買東西 瀏覽:39
python高手之路pdf 瀏覽:84
iphone怎麼像安卓一樣返回 瀏覽:162
linuxtomcat埠佔用怎麼辦 瀏覽:722
表演pdf 瀏覽:923
微信如何更換伺服器 瀏覽:861
android使用html5 瀏覽:460
python可視化極坐標 瀏覽:386
程序員喜歡綠色 瀏覽:732
php編譯的主流軟體 瀏覽:366
操作系統伺服器版本下載地址 瀏覽:247
手機管家加密圖片打不開 瀏覽:841