Android 相机预览教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10896940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android camera preview tutorial
提问by Lasse
I have a litte problem with a tutorial that I follow. I want to make a android application with a camera preview, but until now I haven't found any good tutorial that show how to do it. Here is the link The tutorialI'm not quite shure if I can use the "camera with intent" insted of the "camera preveiew" ? What do I do.
我遵循的教程有一个小问题。我想制作一个带有相机预览的 android 应用程序,但直到现在我还没有找到任何好的教程来展示如何做到这一点。这是链接教程如果我可以使用“相机预览”中的“有意图的相机”,我不太确定?我该怎么办。
Thanks :)
谢谢 :)
回答by Vipul Shah
Below Tutorials will help you.
下面的教程会帮助你。
http://www.vogella.com/articles/AndroidCamera/article.html
http://www.vogella.com/articles/AndroidCamera/article.html
Call inbuilt camera intent to have picture.
调用内置摄像头意图获取图片。
public class demo extends Activity {
Button ButtonClick;
int CAMERA_PIC_REQUEST = 1337;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonClick =(Button) findViewById(R.id.Camera);
ButtonClick.setOnClickListener(new OnClickListener (){
@Override
public void onClick(View view)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == 1337)
{
// data.getExtras()
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Now you have received the bitmap..you can pass that bitmap to other activity
and play with it in this activity or pass this bitmap to other activity
and then upload it to server.
}
else
{
Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
回答by hcpl
I'm currently working on a forkof the CameraPreviewSampleproject. The nice thing about this example is that the github sources are tagged for several steps needed to make the camera preview work.
我目前工作的一个叉的的CameraPreviewSample项目。这个例子的好处是 github 源被标记为使相机预览工作所需的几个步骤。
So if you're looking into that running over the several tags (check readme for details) might be a good idea.
因此,如果您正在研究运行多个标签(查看自述文件了解详细信息)可能是一个好主意。
Another good resource are the training articles from Google. For camera the Android Training Article about Camera controlis best.
另一个很好的资源是来自 Google 的培训文章。对于相机,关于相机控制的Android 培训文章是最好的。
回答by Darish
Using SurfaceView
or TextureView
directly is not recommended, use PreviewView instead.
不推荐直接使用SurfaceView
或TextureView
,使用 PreviewView 代替。
The PreviewView
, part of the CameraX Jetpack library, makes displaying a camera preview easier for developers by providing a developer-friendly, consistent, and stable API across a wide range of Android devices.
在PreviewView
该CameraX Jetpack的图书馆,部分,使得通过在广泛的Android设备提供了一个开发者友好的,一致的,稳定的API显示相机预览开发人员更容易。
In the xml
在 xml
<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:scaleType="fitCenter" />
In your code,
在您的代码中,
// Create a preview use case instance
val preview = Preview.Builder().build()
//camera selector
val cameraSelector : CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
// Bind the preview use case and other needed user cases to a lifecycle
val camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
// Create a surfaceProvider using the bound camera's cameraInfo
val surfaceProvider = previewView.createSurfaceProvider(camera.cameraInfo)
// Attach the surfaceProvider to the preview use case to start preview
preview.setSurfaceProvider(surfaceProvider)
See the complete documentation here.
请参阅此处的完整文档。