如何解决CameraX ImageView 不会显示拍摄的图像
我有两个片段:
Fragment CameraXPicture
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#FF000000"
tools:context=".pkgActivity.Cameraimp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/preview_area"
android:importantForAccessibility="no">
</androidx.camera.view.PreviewView>
<ImageView
android:id="@+id/captureImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/capture"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
PictureCropp
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentCroppImage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//hardcoded for testing reasons
<ImageView
android:id="@+id/takenPicture"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
我的 CameraXPicture Fragment 拍摄一张照片并将照片转发给 FragmentimageCropp:
private Executor executor = Executors.newSingleThreadExecutor();
captureImage.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageCapture.takePicture(executor,new ImageCapture.OnImageCapturedCallback() {
@SuppressLint("UnsafeExperimentalUsageError")
@Override
public void onCaptureSuccess(@NonNull ImageProxy image) {
FragmentCroppImage fragment2 = new FragmentCroppImage();
Bundle args = new Bundle();
args.putByteArray("takenPic",imageProxyToBitmapByteArray(image));// length returns 6489014
fragment2.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt,fragment2);
fragmentTransaction.commit();
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
exception.printstacktrace();
}
});
}
});
private byte[] imageProxyToBitmapByteArray(ImageProxy image) {
ImageProxy.PlaneProxy planeProxy = image.getPlanes()[0];
ByteBuffer buffer = planeProxy.getBuffer();
byte[] bytes = new byte[buffer.remaining()];
return bytes;
}
我的 FragmentimageCropp 只有显示图像的工作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_ParaM1);
mParam2 = getArguments().getString(ARG_ParaM2);
bitmapAsArray = getArguments().getByteArray("takenPic");
Log.e("received length",""+bitmapAsArray.length); //receives 6489014
}
}
ImageView takenPicture;
byte[] bitmapAsArray = new byte[3]; //testvalue
@Override
public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
takenPicture = view.findViewById(R.id.takenPicture);
takenPicture.setimageBitmap(BitmapFactory.decodeByteArray(bitmapAsArray,bitmapAsArray.length));
}
问题是 ImageView 不会显示图像。 ImageView 保持“空”。
问题是我不知道问题可能是什么。是将图片转换为bytearray 还是因为bytearray 转换为bitmap 导致图片出错?
我使用 bytearray 的原因是传输对象(代理图像)需要更长的时间,我想尽可能防止长时间的延迟。
编辑 似乎转换本身不是问题吧知道。我尝试显示硬编码的图片,但没有显示
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。