gif android. 耗资源,Android MP4转GIF
[Java] 純文本查看 復制代碼/**
* [url=home.php?mod=space&uid=952169]@Param[/url] gifPath gif圖片的存儲路徑
* @param begin 開始時間
* @param end 結(jié)束時間
* @param fps
* @param speed
* @param gifWidth gif的導出寬度
* @param gifHeight gif的導出長度
* @param videoState 視頻是橫屏還是豎屏的
* @param needRoate 是否需要旋轉(zhuǎn)
* @param rotateAngle 旋轉(zhuǎn)角度
*/
public void encoder(final String gifPath, final long begin, final long end, final int fps,
final int speed, final int gifWidth, final int gifHeight, final int videoState, final boolean needRoate, final int rotateAngle) {
if (begin > duration) {
// throw new RuntimeException("開始時間不能大于視頻時長");
callBack.erroCallBack("開始時間不能大于視頻時長");
return;
}
if (end <= begin) {
// throw new RuntimeException("開始時間大于結(jié)束時間");
callBack.erroCallBack("開始時間大于結(jié)束時間");
return;
}
Thread thread = new Thread() {
@Override
public void run() {
super.run();
long endTime = duration;
if (end < duration) {
endTime = end;
}
long time1 = System.currentTimeMillis();
videoExtractor.seekTo(begin * 1000, trackIndex); // 精確定位到指定幀(定位到指定開始時間幀)
FastYUVtoRGB fastYUVtoRGB = new FastYUVtoRGB(context);
String mime = format.getString(MediaFormat.KEY_MIME);
MediaCodec videoDecoder = null;
try {
videoDecoder = MediaCodec.createDecoderByType(mime);
} catch (IOException e) {
e.printStackTrace();
}
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
int width = format.getInteger(MediaFormat.KEY_WIDTH);
int height = format.getInteger(MediaFormat.KEY_HEIGHT);
videoDecoder.configure(format, null, null, 0);
videoDecoder.start();
GIFEncoder encoder = null;
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
int f = fps;
if (f <= 0) {
f = 15;
}
int s = speed;
if (s <= 0) {
s = f;
}
long frameTime = 1000 / f;
long startTime = begin;
while (true) {
int run = extractorVideoInputBuffer(videoExtractor, videoDecoder);
if (run == 1) {
int outIndex = videoDecoder.dequeueOutputBuffer(info, 500000);
if (outIndex >= 0) {
long time = info.presentationTimeUs / 1000;
if (time >= begin && time <= endTime) {
if (time >= startTime) {
Image image = videoDecoder.getOutputImage(outIndex); // 輸出
Bitmap bitmap = fastYUVtoRGB.convertYUVtoRGB(getDataFromImage(image), width, height);
int videoImageState = MImageUtil.getPicDirection(image.getHeight(), image.getWidth());
if (needRoate) { // 是否需要旋轉(zhuǎn),解決自動旋轉(zhuǎn)錯誤的問題
bitmap = PhotoBitmapUtils.rotateBitmap(bitmap, rotateAngle);
} else {
// LogUtil.i("視頻導出圖片:" + videoImageState);
// LogUtil.i("原視頻圖片:" + videoState);
if (videoImageState != videoState) { // 橫豎屏不一致,默認自動旋轉(zhuǎn)90°
bitmap = PhotoBitmapUtils.rotateBitmap(bitmap, 90);
}
}
if (gifWidth != -1 && gifHeight != -1) { // 創(chuàng)建新的位圖
bitmap = Bitmap.createScaledBitmap(bitmap, gifWidth, gifHeight, true);
} else {
bitmap = Bitmap.createScaledBitmap(bitmap, width / 4, height / 4, true);
}
if (encoder == null) {
encoder = new GIFEncoder();
encoder.setFrameRate(s);
encoder.init(bitmap);
encoder.start(gifPath);
} else {
encoder.addFrame(bitmap);
}
int p = (int) ((startTime - begin) * 100 / (endTime - begin));
LogUtil.d("p = " + p);//進度
if (callBack != null) {
callBack.currentProgress(p);
}
startTime += frameTime;
}
}
videoDecoder.releaseOutputBuffer(outIndex, true /* Surface init */);
if (time >= endTime) {
break;
}
}
} else if (run == -1) {
break;
}
}
if (encoder != null) {
encoder.finish();
}
LogUtil.d("encoder->time = " + (System.currentTimeMillis() - time1));
LogUtil.d("over");
LogUtil.d("gif_path:" + gifPath);
if (callBack != null) {
callBack.onSuccess(System.currentTimeMillis() - time1, gifPath);
}
videoDecoder.stop();
videoDecoder.release();
MFileUtil.clearAllCache(context); // 清除緩存
}
};
thread.start();
}
總結(jié)
以上是生活随笔為你收集整理的gif android. 耗资源,Android MP4转GIF的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android标尺自定义view,and
- 下一篇: 苹果审查非理性高价App 应用商店内购价