如何在不保存/显示图像的情况下拍照
Posted
技术标签:
【中文标题】如何在不保存/显示图像的情况下拍照【英文标题】:How to take pictures without saving / displaying image 【发布时间】:2014-04-17 13:31:59 【问题描述】:我正在与 Eclipse 合作开发适用于 android 的自定义相机应用程序。我已经弄清楚如何显示相机的预览,但我不知道如何在不保存图像的情况下拍照。拍摄照片时,我不想保存图像或将其显示到屏幕上,我只想将其保存为我可以在应用程序中使用的数据类型,并在拍摄另一张照片时覆盖。
编辑:
public class MainActivity extends Activity
private Camera mCamera;
private CameraPreview mCameraPreview;
private Bitmap picture;
private static Uri mImageCaptureUri;
private static Bitmap photo = null;
private byte[] image;
private int PICK_FROM_CAMERA = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
@Override
protected void onCreate(Bundle savedInstanceState)
releaseCameraAndPreview();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode != Activity.RESULT_OK) return;
else
Bundle extras = data.getExtras();
if (extras != null)
photo = extras.getParcelable("data");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
image = bos.toByteArray();
File f = new File(mImageCaptureUri.getPath());
if (f.exists())
f.delete();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
//creates camera class for functions
private Camera getCameraInstance()
Camera camera = null;
try
camera = Camera.open();
catch (Exception e)
Log.v("Schweigen", "camera will not open");
return camera;
//releases camera on completion
private void releaseCameraAndPreview()
if (mCamera != null)
mCamera.release();
mCamera = null;
ShutterCallback shutterCallback = new ShutterCallback()
public void onShutter()
Log.v("Schweigen", "onShutter");
;
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback()
public void onPictureTaken(byte[] data, Camera camera)
Log.v("Schweigen", "onPictureTaken");
;
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback()
public void onPictureTaken(byte[] data, Camera camera)
//picture = BitmapFactory.decodeByteArray(data , 0, data.length);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
catch (ActivityNotFoundException e)
e.printStackTrace();
;
【问题讨论】:
【参考方案1】:从图片中创建一个Uri
。
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uriSavedImage=Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/image.jpg"));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
编辑:之后您可以将其存储在byte[]
中。
正如我告诉你的,你必须从相机中获取 Intent
并将其设置为 Uri
:
private static Uri mImageCaptureUri;
private static Bitmap photo = null;
private byte[] image;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
catch (ActivityNotFoundException e)
e.printStackTrace();
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode != Activity.RESULT_OK) return;
else
Bundle extras = data.getExtras();
if (extras != null)
photo = extras.getParcelable("data");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
image = bos.toByteArray();
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
【讨论】:
使用 byte[] 是我现在想要做的,我只是无法将它转换成位图格式或任何其他有用的格式(我正在尝试:***.com/questions/7620401/…) 我编辑了我的答案以包含您的建议,在这种情况下,图像将存储在文件名 image 下(我认为)。我仍然不明白这将如何解决你 你试过运行它吗?你能发布你的logcat吗 我不明白为什么这是相关的。 URI 和文件路径不一样吗?它仍在保存图像。【参考方案2】:直接拍照的API是here。
【讨论】:
以上是关于如何在不保存/显示图像的情况下拍照的主要内容,如果未能解决你的问题,请参考以下文章
如何在不保存原始图像的情况下保存图像 URL 的裁剪图像? (在 Rails 中使用 Paperclip 或其他插件)
如何在不使用画布的情况下将整个 div 数据转换为图像并将其保存到目录中?