当我设置
javasetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
或java setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Camera2运行正常,并从imageavailablelistener保存图像。
当我设置java setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
或java setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
时
它将退出应用程序并且无法保存图像
YUV图像到位图方法归功于link
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
final Image image = reader.acquireLatestImage();
if (image == null)
return;
BitmapFunctions bitmapFunctions = new BitmapFunctions();
// QrDecoder qrDecoder = new QrDecoder();
//Convert YUV to RGB and return bitmap
Bitmap bitmap = bitmapFunctions.mediaImageToBitmap(image, activity);
public class BitmapFunctions {
private Allocation allocationYuv;
private Allocation allocationRgb;
private RenderScript rs;
public Bitmap mediaImageToBitmap(Image image, Context context) {
final ByteBuffer yuvBytes = imageToByteBuffer(image);
// Convert YUV to RGB
rs = RenderScript.create(context);
Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
allocationRgb = Allocation.createFromBitmap(rs, bitmap);
allocationYuv = Allocation.createSized(rs, Element.U8(rs), yuvBytes.array().length);
allocationYuv.copyFrom(yuvBytes.array());
ScriptIntrinsicYuvToRGB scriptYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
scriptYuvToRgb.setInput(allocationYuv);
scriptYuvToRgb.forEach(allocationRgb);
allocationRgb.copyTo(bitmap);
return bitmap;
}
public void release() {
allocationYuv.destroy();
allocationRgb.destroy();
rs.destroy();
}
private ByteBuffer imageToByteBuffer(final Image image) {
final Rect crop = image.getCropRect();
final int width = crop.width();
final int height = crop.height();
final Image.Plane[] planes = image.getPlanes();
final byte[] rowData = new byte[planes[0].getRowStride()];
final int bufferSize = width * height * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;
final ByteBuffer output = ByteBuffer.allocateDirect(bufferSize);
int channelOffset = 0;
int outputStride = 0;
for (int planeIndex = 0; planeIndex < 3; planeIndex++) {
if (planeIndex == 0) {
channelOffset = 0;
outputStride = 1;
} else if (planeIndex == 1) {
channelOffset = width * height + 1;
outputStride = 2;
} else if (planeIndex == 2) {
channelOffset = width * height;
outputStride = 2;
}
final ByteBuffer buffer = planes[planeIndex].getBuffer();
final int rowStride = planes[planeIndex].getRowStride();
final int pixelStride = planes[planeIndex].getPixelStride();
final int shift = (planeIndex == 0) ? 0 : 1;
final int widthShifted = width >> shift;
final int heightShifted = height >> shift;
buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift));
for (int row = 0; row < heightShifted; row++) {
final int length;
if (pixelStride == 1 && outputStride == 1) {
length = widthShifted;
buffer.get(output.array(), channelOffset, length);
channelOffset += length;
} else {
length = (widthShifted - 1) * pixelStride + 1;
buffer.get(rowData, 0, length);
for (int col = 0; col < widthShifted; col++) {
output.array()[channelOffset] = rowData[col * pixelStride];
channelOffset += outputStride;
}
}
if (row < heightShifted - 1) {
buffer.position(buffer.position() + rowStride - length);
}
}
}
return output;
}
2019-06-19 17:04:47.551 23356-23423/com.dsonic.datasonicscanner E/AndroidRuntime: FATAL EXCEPTION: CameraBackground
Process: com.dsonic.datasonicscanner, PID: 23356
java.lang.IllegalStateException: buffer is inaccessible
at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:219)
at com.dsonic.dsoniccamera2lib.Camera2.BitmapFunctions.imageToByteBuffer(BitmapFunctions.java:105)
at com.dsonic.dsoniccamera2lib.Camera2.BitmapFunctions.mediaImageToBitmap(BitmapFunctions.java:36)
at com.dsonic.dsoniccamera2lib.Camera2.Camera2Manager$4.onImageAvailable(Camera2Manager.java:302)
at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:812)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.os.HandlerThread.run(HandlerThread.java:65)
ctrl+f
查找错误行
BitmapFunctions.java:105是channelOffset + = outputStride;
BitmapFunctions.java:36是位图bitmap = Bitmap.createBitmap(image.getWidth(),image.getHeight(),Bitmap.Config.ARGB_8888);
Camera2Manager.java:302是位图位图= bitmapFunctions.mediaImageToBitmap(image,activity);
现在一切正常,相同的代码不变。使用相同的电缆,但电话今天早上重新启动并添加了Log.e("Image Size", "Width = " + image.getWidth() + " Height = " + image.getHeight());
。
我将其注释掉后,它又自动退出了。现在,我删除了该评论,该评论不再起作用。 WTF?
有人可以向我解释吗?
答案 0 :(得分:0)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)
而不是设置它。
将其更改为强制在清单中定向。不知道为什么这样做
希望有人可以向我解释。