【发布时间】:2015-08-08 14:27:54
【问题描述】:
我拍照并想将其放入另一个 Activity 的 ImageView 中,但出现了问题。这是我的代码:
Camera mCamera;
//Some Code
public void onClick(View v) {
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
}
};
Camera.PictureCallback rawCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
Intent inCapture = new Intent(MainActivity.this, SecondActivity.class);
//Actually, I don't know where to put it: here or in jpegCallback
inCapture.putExtra("captured", data);
startActivity(inCapture);
}
};
Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//My attempt to load the result into ImageView in the same Activity - didn't work too
if (data != null) {
ByteArrayInputStream imageStream = new ByteArrayInputStream(
data);
Bitmap theImage = BitmapFactory
.decodeStream(imageStream);
imageView.setBackground(null);
imageView.setBackground(new BitmapDrawable(
getResources(), theImage));
}
}
};
它没有运行 SecondActivity,我不知道如何在 onClick() 中声明我的意图并能够将 byte[] 数据放入其中(不同的类,我不能将意图设为静态 - 至少,我不能't)。有没有更好的方法来捕捉照片并将那个字节 [] 传递给另一个 Activity?
附:我知道还有较新的 Camera2,但到目前为止我不明白如何使用它。
【问题讨论】:
-
“更好的方法”是不使用单独的活动。您不能轻易地将照片传递给另一个活动,因为它对于
Intent额外的内容来说太大了。改为使用一个活动并单独的片段。 -
@CommonsWare 那么如何将我的位图放在同一个 Activity 的 imageView 中?我试过(将 SurfaceView 设置为 INVISIBLE 并将转换后的 byte[] 作为 Bitmap 设置为 ImageView 但没有结果
-
"那么如何将我的位图放在同一个Activity的imageView中?" -- 在
BitmapFactory上使用decodeByteArray()得到Bitmap,然后在ImageView上调用setImageBitmap()。不要使用ByteArrayOutputStream,也不要使用setBackground()。 -
@CommonsWare 我做到了,一切都一样,但我在日志中看到“位图太大,无法上传到纹理中(3600x4800,max=4096x4096)。然后我创建了它的 ScaledBitmap(360x480 ),但我得到了同样的信息。
-
"位图太大,无法上传到纹理中 (3600x4800, max=4096x4096)" -- 使用
BitmapFactory.Options和合适的inSampleSize在使用decodeByteArray()时对图像进行下采样。例如,尝试inSampleSizeof 2。
标签: android camera android-camera android-camera-intent