Android调用手机摄像头

根据<第一行代码>进行改写:

布局文件,只有一个按钮,和一个Imageview,imageview用于显示拍下后的图片
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/take_photo"
            android:text="Take Photo"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/picture"/>




    </LinearLayout>
    


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.choosepictest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    public static final int TAKE_PHOTO=1;

    private Button takePhoto;
    private ImageView picture;
    private Uri imageUri;

    public static File tempFile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        takePhoto = (Button) findViewById(R.id.take_photo);
        picture = (ImageView) findViewById(R.id.picture);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tempFile=new File(getExternalCacheDir(),"output_image.jpg");
                if(tempFile.exists())
                {
                    tempFile.delete();
                }
                try {
                    tempFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
                {
                    imageUri= FileProvider.getUriForFile(MainActivity.this,
                            "com.example.choosepictest.fileprovider",tempFile);
                }else{
                    Uri.fromFile(tempFile);
                }
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode,requestCode,data);
        switch (requestCode)
        {
            case TAKE_PHOTO:
            if(resultCode==Activity.RESULT_OK)
            {
                Bitmap bitmap= null;
                try {
                    bitmap = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(imageUri));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                picture.setImageBitmap(rotateIfRequired(bitmap));
            }
            break;
            default:
                break;
        }

    }

    private Bitmap rotateIfRequired(Bitmap bitmap)
    {
        String path=tempFile.getPath();
        ExifInterface exif = null;
        try {
            exif=new ExifInterface(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation= exif.getAttributeInt(ExifInterface.TAG_EXIF_VERSION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation)
        {
            case ExifInterface.ORIENTATION_ROTATE_90:
                bitmap=rotateBitmap(bitmap,90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                bitmap=rotateBitmap(bitmap,180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                bitmap=rotateBitmap(bitmap,270);
                break;
            default:
                break;
        }
        return bitmap;

    }

    private Bitmap rotateBitmap(Bitmap bitmap,int degree)
    {
        Matrix matrix=new Matrix();
        matrix.postRotate((float)degree);
        Bitmap rotateBitmap=Bitmap.createBitmap(bitmap,0,0,
                bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        bitmap.recycle();
        return bitmap;


    }

}

活动管理(mainfest):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.choosepictest">




    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ChoosePicTest">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:authorities="com.example.choosepictest.fileprovider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>

        </provider>

    </application>

</manifest>

用于指定路径file_paths.xml:

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="my_images"
        path="/"/>
</paths>

效果图:
在这里插入图片描述
这是虚拟机,所以啥也看不到,真机就可以看到了

### 回答1: Qt for Android可以通过Qt Multimedia模块来调用安卓手机摄像头实现拍照和录像功能。 首先,需要在.pro文件中添加multimedia模块的引用,即将`QT += multimedia`添加到.pro文件中。 接下来,创建一个QCamera对象,可以使用默认摄像头或指定摄像头。通过调用`QCameraInfo::defaultCamera()`可以获取默认摄像头的信息,或者使用`QCameraInfo::availableCameras()`获取所有可用摄像头列表。然后,调用`setCaptureMode()`方法来设置摄像头的捕获模式,可以选择使用`QCamera::CaptureStillImage`来拍照,或使用`QCamera::CaptureVideo`来录制视频。 如果需要显示摄像头的即时预览画面,可以创建一个QCameraViewfinder对象,并将其设置为QCamera的视图finder,然后将QCameraViewfinder设置为显示在窗口上。可以使用QGraphicsView或QWidget来显示摄像头的预览画面。 在拍照时,可以使用QCameraCaptureSession或直接使用QCamera的capture()方法来捕获静态图像。捕获的图像可以使用QCameraImageCapture类获取,并保存到本地文件中。 在录制视频时,可以使用QMediaRecorder来进行视频录制,首先创建一个QMediaRecorder对象,并使用setMedia()方法设置录制的媒体文件名和格式。然后,设置视频编码器、分辨率、比特率等参数,并调用record()方法开始录制,调用stop()方法停止录制。 最后,记得在AndroidManifest.xml文件中添加相应的权限,例如访问相机、录音和存储等权限。 通过以上步骤,就可以在Qt for Android中成功调用手机摄像头实现拍照和录像功能了。 ### 回答2: 在Qt for Android开发中,要调用手机摄像头,可以使用Qt Multimedia模块中的QCamera类。 首先,需要在.pro文件中添加对Multimedia模块的依赖: ``` QT += multimedia ``` 然后,在代码中引入QCamera和QCameraViewfinder类: ``` #include <QCamera> #include <QCameraViewfinder> ``` 接下来,创建一个QCamera对象并设置使用后置摄像头: ``` QCamera* camera = new QCamera; camera->setCaptureMode(QCamera::CaptureStillImage); // 设置为拍照模式 camera->setCaptureMode(QCamera::CaptureVideo); // 设置为录像模式 QCameraViewfinder* viewfinder = new QCameraViewfinder; camera->setViewfinder(viewfinder); QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); foreach (const QCameraInfo& cameraInfo, cameras) { if (cameraInfo.position() == QCamera::BackFace) { // 后置摄像头 camera->setCamera(cameraInfo); // 设置为后置摄像头 break; } } ``` 然后,可以在需要调用摄像头的地方,调用QCamera的相关方法,比如开始预览、拍照或录像: ``` camera->start(); camera->searchAndLock(); camera->unlock(); camera->searchAndCapture(); ``` 最后,需要在界面上显示摄像头预览画面,可以将QCameraViewfinder设置为QWidget的子控件,并将其显示出来: ``` QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(viewfinder); setLayout(layout); ``` 以上就是利用Qt for Android调用手机摄像头的基本步骤,开发者可以根据实际需求,进一步对摄像头功能进行扩展和定制。 ### 回答3: 在Qt中调用Android手机摄像头可以通过Qt Multimedia模块来实现。首先,确保已经正确配置了Qt for Android开发环境,并在.pro文件中添加了对Qt Multimedia模块的依赖,类似于:QT += multimedia。 接下来,创建一个Qt Quick界面来显示摄像头捕获的图像。可以使用Camera类型的对象来控制摄像头,并将摄像头的图像显示在Qt Quick界面上。 具体的步骤如下: 1. 在Qt Creator中创建一个新的Qt Quick项目。 2. 在qml文件中添加一个Item,用于显示摄像头图像,例如: Item { id: cameraView width: 640 height: 480 visible: camera.available anchors.centerIn: parent } 3. 在C++代码中创建一个Camera对象并连接到cameraView的source属性上: QCamera *camera = new QCamera(this); camera->setViewfinder(cameraView); camera->start(); 4. 在AndroidManifest.xml文件中添加相机权限,例如: <uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera"/> <uses-feature android:name="android.hardware.camera.autofocus"/> <uses-feature android:name="android.hardware.camera.flash"/> 5. 在运行项目之前,将生成的apk安装到Android手机上,并确保手机上有可用的摄像头。 通过以上步骤,你的Qt for Android应用程序现在可以调用Android手机摄像头并显示捕获的图像了。你还可以通过QCameraViewfinderSettings类来更改摄像头的设置,例如分辨率、帧率等。除了显示图像,你还可以使用QCamera类提供的其他函数来控制摄像头的拍照、录像等功能。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油小杜(接qt定制功能,单模块开发等)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值