問(wèn)題描述
我正在使用帶有 Eclipse 的 Android 2.2.
I'm using Android 2.2 with Eclipse.
我想制作一個(gè)捕獲視頻的應(yīng)用程序,并且對(duì)于每一幀,將其作為位圖發(fā)送到處理它的方法并返回一個(gè)新位圖并顯示處理后的位圖.
I would like to make an application that captures video, and for each frame, its send it as a bitmap to a method that processes it and returns a new bitmap and shows the processed bitmap.
我對(duì) Android 不是很熟悉,所以請(qǐng)誰(shuí)能給我發(fā)一份我需要查看的資源來(lái)做這樣的事情?
I am not very familiar with Android, so please, can anyone send me to the resources I need to look at to do such a thing?
推薦答案
使用Android SDK完成以下步驟很簡(jiǎn)單:
It is simple enough to accomplish the following steps using the Android SDK:
- 將相機(jī)中的預(yù)覽幀捕獲為位圖數(shù)據(jù).
Camera.PreviewCallback
將返回一個(gè)byte[]
數(shù)據(jù),以多種可能的圖像格式表示幀. - 修改像素?cái)?shù)據(jù).由于數(shù)據(jù)以原始字節(jié)的形式返回,因此對(duì)數(shù)據(jù)進(jìn)行調(diào)整相對(duì)容易......這里的困難在于為您想要執(zhí)行的特定圖像處理應(yīng)用算法.沒(méi)有任何內(nèi)置效果(4.0 之前)可以簡(jiǎn)單地應(yīng)用于圖像,因此您必須自己編寫(xiě).
- 還可以將數(shù)據(jù)解碼為
Bitmap
對(duì)象,以便更輕松地處理像素.在 2.2 中,您可以選擇使用 NDK 和 jnigraphics 在本機(jī)代碼中處理位圖的像素,這比 Java 層要快得多. - 獲取生成的
Bitmap
的內(nèi)容并顯示它.對(duì)于快速移動(dòng)的數(shù)據(jù),您可能希望在SurfaceView
上顯示它;使用視圖包含的SurfaceHolder
提供的lockCanvas()
和unlockCanvasAndPost()
方法.
- Capture the preview frames from the camera as bitmapped data.
Camera.PreviewCallback
will return abyte[]
of data representing the frame in a number of possible image formats. - Modify the pixel data. Since the data comes back as raw bytes, making adjustments to the data is relatively easy...the difficulty here is applying an algorithm for the particular image processing you want to do. There aren't any built-in effects (pre-4.0) that can be applied simply to images so you will have to write your own.
- It is also possible to decode the data into a
Bitmap
object to make working with the pixels easier. In 2.2, you have the option of using the NDK and jnigraphics to work with a Bitmap's pixels in native code, which is significantly faster than at the Java layer. - Take the contents of your resultant
Bitmap
and display it. For fast moving data, you would want to display this on aSurfaceView
; using thelockCanvas()
andunlockCanvasAndPost()
methods available from theSurfaceHolder
that view contains.
如果這就是您想做的全部,您可以輕松完成.但是,這與捕獲視頻不同.Android 目前不提供掛鉤讓您將幀實(shí)時(shí)流式傳輸?shù)骄幋a的視頻容器(MPEG4、3GP 等)中.它的視頻捕獲功能被緊緊地包裹在 MediaRecorder
中,它控制著從幀捕獲到編寫(xiě)編碼視頻的整個(gè)過(guò)程.您將需要一個(gè)第三部分庫(kù),例如 FFMPEG(已在 Android 應(yīng)用程序中多次使用 NDK 層構(gòu)建和運(yùn)行)來(lái)協(xié)助修改幀的編碼過(guò)程.
If this is all you are wanting to do, you could accomplish this with little difficulty. However, this is not the same as capturing video. Android does not currently provide hooks for you to stream frames out into an encoded video container (MPEG4, 3GP, etc.) in real-time. It's video capture capabilities are wrapped up tightly into the MediaRecorder
which controls the process from frame capture all the way to writing the encoded video. You would need a 3rd part library such as FFMPEG (which has been built and run using the NDK layer a number of times in Android applications) to assist in the encoding process of your modified frames.
我知道您的目標(biāo)是 2.2,但 Android 4.0 確實(shí)在這方面提供了一些緩解,因?yàn)樗麄儼l(fā)布了新版本的 NDK,它允許人們?cè)趶牧髦凶x取圖像數(shù)據(jù)然后將其交給之前有更多發(fā)言權(quán)表示層.但是,我沒(méi)有花足夠的時(shí)間來(lái)了解它是否適??合您的情況.
I know you have a 2.2 target, but Android 4.0 does provide some relief here as they have released a new version of the NDK which allows one to have more say in what happens when reading image data from a stream before handing it to the presentation layer. However, I have not spent enough time with it to know whether it could be recommended for your situation.
這篇關(guān)于Android中的視頻處理的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!