Android 调用手机系统照相机拍照

Android 调用手机系统照相机拍照 并用ImageView显示    此方法获得的是拍照的原图片而非缩略图。

1.首先是布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@null"
        android:contentDescription="@string/app_name" 
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/update_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:text="拍照" />

</RelativeLayout>

2.接着是MainActivity 

package com.updateimage;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {

	private Button btn;
	private ImageView iv;
	private File file;
	private Bitmap photo;
	private static final int cameraCode = 0;

	public static int photoW, photoH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.update_btn);
		iv = (ImageView) findViewById(R.id.iv);

		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				takePictures(1);

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.d("TAG4", "--requestCode--:" + requestCode + "--resultCode--:"
				+ resultCode);
		if (requestCode == cameraCode) {
			if (file != null && file.exists()) {
				Log.d("TAG4", "-----照片地址:-----=" + file.getPath());
				photo = zoomPhoto(file.getPath());
				iv.setImageBitmap(photo);

			}

		}
	}

	/**
	 * 拍照
	 */
	private void takePictures(int n) {
		// 释放Bitmap对象 防止内存溢出
		destoryBitmap();
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			String saveDir = Environment.getExternalStorageDirectory()
					+ "/temple";
			File dir = new File(saveDir);
			if (!dir.exists()) {
				dir.mkdir();
			}
			file = new File(saveDir, "temp" + n + ".jpg");
			file.delete();
			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					Toast.makeText(MainActivity.this, "保存地址为空!",
							Toast.LENGTH_LONG).show();
					return;
				}
			}
			Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
			// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
			startActivityForResult(intent, cameraCode);
		} else {
			Toast.makeText(MainActivity.this, "请插入SD卡", Toast.LENGTH_LONG)
					.show();
		}
	}

	/**
	 * 缩放图片(显示)
	 * 
	 * @param bitmap
	 * @return
	 */
	private Bitmap zoomPhoto(String path) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;

		// 获取这个图片的宽和高
		Bitmap bitmap = BitmapFactory.decodeFile(path, options);
		photoW = options.outWidth;
		photoH = options.outHeight;
		System.out.println("photoW=" + photoW + "----------" + "photoH="
				+ photoH);
		options.inJustDecodeBounds = false;
		// 计算缩放比例
		int be = (int) (options.outHeight / 200.0);
		if (be <= 0)
			be = 1;
		options.inSampleSize = 4;// 图片长宽各缩小至四分之一
		// 重新读入图片,注意这次要把options.inJustDecodeBounds设为false
		bitmap = BitmapFactory.decodeFile(path, options);
		return bitmap;
	}

	/**
	 * 释放Bitmap对象 防止内存溢出
	 */
	private void destoryBitmap() {
		if (photo != null && !photo.isRecycled()) {
			// photo.recycle();
			photo = null;
		}
	}

}

PS:手机拍照需要添加权限:<uses-permission android:name="android.permission.CAMERA" />

       向SD卡写东西需要权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

运行项目即可成功!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Qt for Android 6中调用安卓照相机可以通过使用Qt的Multimedia模块中的Camera类来实现。首先,需要在.pro文件中添加相机模块的引用,例如: QT += multimedia 然后,在需要调用照相机的地方,可以使用以下代码来实现: #include <QCamera> #include <QCameraViewfinder> #include <QCameraImageCapture> // 创建相机对象 QCamera camera; // 创建相机取景器对象,并设置为相机的视图 QCameraViewfinder viewfinder; camera.setViewfinder(&viewfinder); // 创建图像捕捉对象,并设置为相机的图像捕捉 QCameraImageCapture imageCapture; camera.setImageCapture(&imageCapture); // 设置相机的图像保存路径 imageCapture.setCaptureDestination(QCameraImageCapture::CaptureToFile); imageCapture.setCaptureDestination(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)); // 连接图像捕获的信号和槽 QObject::connect(&imageCapture, SIGNAL(imageCaptured(int, const QImage&)), this, SLOT(onImageCaptured(int, const QImage&))); // 启动相机 camera.start(); 上述代码中,首先创建相机对象camera,并创建相机取景器对象viewfinder,并将其设置为相机视图。然后,创建图像捕捉对象imageCapture,并设置为相机的图像捕捉。使用setCaptureDestination方法可以设置图像保存的路径,这里设置为手机图片存储路径。最后,使用QObject::connect连接图像捕获的信号和槽,并调用camera.start()启动相机。 通过以上步骤,就可以在Qt for Android 6中调用安卓照相机实现拍照功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值