我正在尝试从我的应用程序中的一些图像中编码视频。
我正在使用 jcodec 库来实现我的任务。
这是我的代码:
public class Encoder extends AsyncTask<String, Integer, Integer> {
private static final String TAG = "ENCODER";
private Context context;
private ArrayList<PresentationItem > mData;
public Encoder(Context context, ArrayList<PresentationItem> mData) {
this.context = context;
this.mData = mData;
}
protected Integer doInBackground(String... params) {
File gallery_folder = new File(Environment.getExternalStorageDirectory(),
"Test" );
SequenceEncoder se = null;
try {
se = new SequenceEncoder(new File(gallery_folder,
"jcodec_enc.mp4"));
for (int i = 0; i<mData.size(); i++) {
Bitmap frame = Utils.getBitmap(mData.get(i).getImagePath(), 100, 100);
se.encodeImage(frame);
// publishProgress(i);
}
se.finish();
} catch (IOException e) {
Log.e(TAG, "IO", e);
}
return 0;
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
}
}
Utils.getBitmap 方法:
public static Bitmap getBitmap(String imageFile, int targetW, int targetH)
{
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap b = BitmapFactory.decodeFile(imageFile, bmOptions);
return b;
}
这是我的 logcat 输出:
04-27 07:20:14.315: E/AndroidRuntime(2743): FATAL EXCEPTION: AsyncTask #2
04-27 07:20:14.315: E/AndroidRuntime(2743): java.lang.RuntimeException: An error occured while executing doInBackground()
04-27 07:20:14.315: E/AndroidRuntime(2743): at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-27 07:20:14.315: E/AndroidRuntime(2743): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.lang.Thread.run(Thread.java:856)
04-27 07:20:14.315: E/AndroidRuntime(2743): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=13872; index=13940
04-27 07:20:14.315: E/AndroidRuntime(2743): at org.jcodec.scale.RgbToYuv420j.rgb2yuv(RgbToYuv420j.java:72)
04-27 07:20:14.315: E/AndroidRuntime(2743): at org.jcodec.scale.RgbToYuv420j.transform(RgbToYuv420j.java:38)
04-27 07:20:14.315: E/AndroidRuntime(2743): at org.jcodec.api.SequenceEncoder.encodeNativeFrame(SequenceEncoder.java:72)
04-27 07:20:14.315: E/AndroidRuntime(2743): at org.jcodec.api.android.SequenceEncoder.encodeImage(SequenceEncoder.java:24)
04-27 07:20:14.315: E/AndroidRuntime(2743): at com.moblin.huggies.utils.Encoder.doInBackground(Encoder.java:56)
04-27 07:20:14.315: E/AndroidRuntime(2743): at com.moblin.huggies.utils.Encoder.doInBackground(Encoder.java:22)
04-27 07:20:14.315: E/AndroidRuntime(2743): at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-27 07:20:14.315: E/AndroidRuntime(2743): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-27 07:20:14.315: E/AndroidRuntime(2743): ... 5 more
此行中的错误原因:
se.encodeImage(frame);
找不到发生这种情况的原因。
此外,我使用的代码取自 jcode 示例。