机器人:以摄像机图像无[储存" /"删除"确认 [英] android: Take camera picture without "save" / "delete" confirmation
问题描述
我想显示使用来自相机在ImageView的拍摄图像
I want to display an image taken from the camera in an ImageView using
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
这工作正常,到目前为止,但使用所选择的相机应用程序中的对话框,用户拍摄照片后(大概是从应用程序)出现询问是否保存或删除拍摄的照片(至少在Android 2.3和4.2使用的默认摄像头应用程序)。
This works fine so far but after the user takes the photo using the chosen camera app a dialog (probably from the app) appears asking whether to save or delete the taken picture (at least on Android 2.3 and 4.2 using the default camera app).
我想为跳过这个额外的对话框,然后直接在ImageView的显示图像时(onActivityResult被调用),因为这意味着对于用户而言,这是不必要的额外的交互步骤,因为他将有possibilty保存或删除的照片我的应用程序。
I would like to skip this extra dialog and directly display the image in the ImageView (when onActivityResult gets called), because it means an extra interaction step for the user, which is unneccessary because he will have the possibilty to save or delete the photo in my app.
使用简单ACTION_IMAGE_CAPTURE意图这是可能的,否则我将需要更复杂的东西像相机preVIEW和SurfaceView为了这个目的?
Is this possible using the simple ACTION_IMAGE_CAPTURE Intent or will I need something more complex like Camera Preview and SurfaceView for this purpose ?
推荐答案
您ca的使用 SurfaceView
来捕捉图像
You ca use the SurfaceView
to capture image
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>
开始用 startActivityForResult
此 Camera_capture
活动和 onActivityResult
你可以得到图像字节
阵列
Start this Camera_capture
activity with startActivityForResult
and onActivityResult
you can get the image as byte
array as
byte[] image = data.getExtras().getByteArray("image_arr");
其中,数据
是接收到的数据。
德$ C C的字节
数组$以位图
用
Decode the byte
array to Bitmap
using
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
image.length);
现在设置该位图
修改
由于有一些问题,同时返回字节[]
的字节[]
应该是保存在一个文件中与该文件的路径应该被发送到previous 活性
使得文件可被读出。
As there is some problem while returning byte[]
, the byte[]
should be save in a file and the path of the file should be sent to the previous Activity
so that file can be read.
在 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);
和接收previous这条路活动
的 onActivityResult
为:
and receive this path in previous Activity
's onActivityResult
as :
String imagePath = data.getExtras().getString("image_path");
这篇关于机器人:以摄像机图像无[储存&QUOT; /&QUOT;删除&QUOT;确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!