gpt4 book ai didi

android - 如何从 Android 中的自定义 CameraView 捕获图像?

转载 作者:IT老高 更新时间:2023-10-28 21:35:10 25 4
gpt4 key购买 nike

我需要捕获图像 屏幕的所需部分

相机捕获图像

当时其他屏幕内容

enter image description here

怎么可能

最佳答案

尝试使用 Surface View创建动态 相机 View 并设置在您需要的部分。

下面的代码试试

变量设置类级别(全局)

Button btn_capture;
Camera camera1;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
public static boolean previewing = false;

onCreate()方法中的以下代码

getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = new SurfaceView(this);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btn_capture = (Button) findViewById(R.id.button1);

surfaceView.setBackgroundResource(R.drawable.your_background_image);

if(!previewing){

camera1 = Camera.open();
if (camera1 != null){
try {
camera1.setDisplayOrientation(90);
camera1.setPreviewDisplay(surfaceHolder);
camera1.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

btn_capture.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

if(camera != null)
{
camera1.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);

}
}
});

以下代码将 onCreate() 放在你的类中。

ShutterCallback myShutterCallback = new ShutterCallback(){

public void onShutter() {
// TODO Auto-generated method stub
}};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

Bitmap correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true);

}};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera1.stopPreview();
previewing = false;
}

if (camera1 != null){
try {
camera1.setPreviewDisplay(surfaceHolder);
camera1.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

camera1.stopPreview();
camera1.release();
camera1 = null;
previewing = false;

}

AndroidManifest.xml 中给予用户权限

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>

也不要忘记类的(实现 SurfaceHolder.Callback )

关于android - 如何从 Android 中的自定义 CameraView 捕获图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15391329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com