gpt4 book ai didi

android:在没有 "save"/ "delete"确认的情况下拍摄相机照片

转载 作者:可可西里 更新时间:2023-11-01 18:45:16 25 4
gpt4 key购买 nike

我想使用

在 ImageView 中显示从相机拍摄的图像
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

这到目前为止工作正常,但在用户使用所选的相机应用程序拍摄照片后,会出现一个对话框(可能来自应用程序)询问是保存还是删除拍摄的照片(至少在 Android 2.3 和 4.2 上使用默认相机应用程序)。

我想跳过这个额外的对话框并直接在 ImageView 中显示图像(当 onActivityResult 被调用时),因为这对用户来说意味着一个额外的交互步骤,这是不必要的,因为他会可以在我的应用程序中保存或删除照片。

这是否可以使用简单的 ACTION_IMAGE_CAPTURE Intent 或为此目的我需要更复杂的东西,例如 Camera Preview 和 SurfaceView?

最佳答案

你不能使用 SurfaceView捕捉图像

package com.camera;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Camera_capture extends Activity implements SurfaceHolder.Callback {

private Camera mCamera;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Button capture_image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
capture_image = (Button) findViewById(R.id.capture_image);
capture_image.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
capture();
}
});
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(Camera_capture.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}

private void capture() {
mCamera.takePicture(null, null, null, new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {
Toast.makeText(getApplicationContext(), "Picture Taken",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("image_arr", data);
setResult(RESULT_OK, intent);
camera.stopPreview();
if (camera != null) {
camera.release();
mCamera = null;
}
finish();
}
});
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.e("Surface Changed", "format == " + format + ", width === "
+ width + ", height === " + height);
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e("Surface Created", "");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("Surface Destroyed", "");
}

@Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
}

布局文件是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_weight="100"
android:layout_height="wrap_content" />

<Button
android:id="@+id/capture_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capture" />

</LinearLayout>

开始这个Camera_capturestartActivityForResult 的 Activity 和 onActivityResult您可以获得 byte 的图像数组为

byte[] image = data.getExtras().getByteArray("image_arr");

哪里data是接收到的数据。

解码byte数组到 Bitmap使用

Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
image.length);

现在设置这个 Bitmap


编辑

返回时出现问题byte[] , byte[]应该保存在一个文件中,文件的路径应该发送到前面的Activity以便可以读取该文件。

onPictureTaken() , 只需添加

String PATH = "Any path to store a file";
try {
FileOutputStream fos=new FileOutputStream(PATH);

fos.write(data);
fos.close();
}
catch (java.io.IOException e) {

}

代替:

intent.putExtra("image_arr", data);

intent.putExtra("image_path", PATH);

并在之前的 Activity 中接收此路径的 onActivityResult作为:

String imagePath = data.getExtras().getString("image_path");

关于android:在没有 "save"/ "delete"确认的情况下拍摄相机照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297606/

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