我正在使用此代码在Imageview中保存图片,但在imageview中的dsave时图像被拉伸.相机预览是完美的,然后单击右图像,但是当我在imageview中设置该图像时,图像被剔除.
public void onPicTaken(byte[] data) {
if (data != null) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
Bitmap scaled = Bitmap.createScaledBitmap(bm, screenHeight, screenWidth, true);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
bm = Bitmap.createBitmap(scaled, 0, 0, w, …Run Code Online (Sandbox Code Playgroud) 我正在使用CameraX代码实验室,即使使用setTargetAspectRatio和setTargetResolution方法,预览中的纵横比也错误。
private fun startCamera() {
// Create configuration object for the viewfinder use case
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
}.build()
...
Run Code Online (Sandbox Code Playgroud)
布局使用的是硬编码的大小,如代码实验室中所示。
<TextureView
android:id="@+id/view_finder"
android:layout_width="640px"
android:layout_height="640px"
...
Run Code Online (Sandbox Code Playgroud)
如果库具有CameraTextureView和属性android:scaleType(类似于的现有属性ImageView),可以将预览调整为预览大小,那就太好了。