资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JAVA代码做视频合成 java 生成视频文件

JAVA 将照片转视频且有动画效果

感觉这应该是前端页面的功能吧,不是后端做的事情。是让照片以幻灯片的方式呈现,这是纯页面效果,后台只负责推送数据,怎么展现是前端的活了吧。

专注于为中小企业提供成都做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业岑溪免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

为什么java合成视频没有报错,但是视频没有成功

java合成视频没有报错,但是视频没有成功原因如下:

1、heightdiffers视频的高度不同导致的,认真查看自己的需要合并的视频文件。

2、发现有些视频是横屏拍摄,有些视频是竖屏拍摄,导致文件高度不同。因此在视频文件在合并的时候报错,解决办法保证需要合并的视频文件是相同高度的视频文件。

新手学习使用Java,尝试着做一个项目使用Java做一个视频图像的处理。

Java图像处理技巧四则

下面代码中用到的sourceImage是一个已经存在的Image对象

图像剪切

对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:

//import java.awt.*;

//import java.awt.image.*;

Image croppedImage;

ImageFilter cropFilter;

CropFilter =new CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API

CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。

图像缩放

对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像

Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。

//其它情况请参考API

灰度变换

下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。

/* GrayFilter.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayFilter extends RGBImageFilter {

int modelStyle;

public GrayFilter() {

modelStyle=GrayModel.CS_MAX;

canFilterIndexColorModel=true;

}

public GrayFilter(int style) {

modelStyle=style;

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

if (modelStyle==GrayModel

else if (modelStyle==GrayModel

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

/* GrayModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayModel extends ColorModel {

public static final int CS_MAX=0;

public static final int CS_FLOAT=1;

ColorModel sourceModel;

int modelStyle;

public GrayModel(ColorModel sourceModel) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=0;

}

public GrayModel(ColorModel sourceModel,int style) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=style;

}

public void setGrayStyle(int style) {

modelStyle=style;

}

protected int getGrayLevel(int pixel) {

if (modelStyle==CS_MAX) {

return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));

}

else if (modelStyle==CS_FLOAT){

return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);

}

else {

return 0;

}

}

public int getAlpha(int pixel) {

return sourceModel.getAlpha(pixel);

}

public int getRed(int pixel) {

return getGrayLevel(pixel);

}

public int getGreen(int pixel) {

return getGrayLevel(pixel);

}

public int getBlue(int pixel) {

return getGrayLevel(pixel);

}

public int getRGB(int pixel) {

int gray=getGrayLevel(pixel);

return (getAlpha(pixel)24)+(gray16)+(gray8)+gray;

}

}

如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。

色彩变换

根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class ReverseColorModel extends ColorModel {

ColorModel sourceModel;

public ReverseColorModel(ColorModel sourceModel) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

}

public int getAlpha(int pixel) {

return sourceModel.getAlpha(pixel);

}

public int getRed(int pixel) {

return ~sourceModel.getRed(pixel);

}

public int getGreen(int pixel) {

return ~sourceModel.getGreen(pixel);

}

public int getBlue(int pixel) {

return ~sourceModel.getBlue(pixel);

}

public int getRGB(int pixel) {

return (getAlpha(pixel)24)+(getRed(pixel)16)+(getGreen(pixel)8)+getBlue(pixel);

}

}

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class ReverseFilter extends RGBImageFilter {

public ReverseFilter() {

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

substituteColorModel(cm,new ReverseColorModel(cm));

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。

下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.*;

import java.awt.image.*;

import javax.swing.*;

import java.awt.color.*;

public class GrayImage extends JFrame{

Image source,gray,gray3,clip,bigimg;

BufferedImage bimg,gray2;

GrayFilter filter,filter2;

ImageIcon ii;

ImageFilter cropFilter;

int iw,ih;

public GrayImage() {

ii=new ImageIcon(\"images/11.gif\");

source=ii.getImage();

iw=source.getWidth(this);

ih=source.getHeight(this);

filter=new GrayFilter();

filter2=new GrayFilter(GrayModel.CS_FLOAT);

gray=createImage(new FilteredImageSource(source.getSource(),filter));

gray3=createImage(new FilteredImageSource(source.getSource(),filter2));

cropFilter=new CropImageFilter(5,5,iw-5,ih-5);

clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));

bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);

MediaTracker mt=new MediaTracker(this);

mt.addImage(gray,0);

try {

mt.waitForAll();

} catch (Exception e) {

}

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代码做视频合成 java 生成视频文件
文章出自:http://cdkjz.cn/article/doiecjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220