我有同样的问题。从cameraview(我使用camerakit lib)保存图像后,通过将其转换为imageview中的位图来显示文件。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraKitView = findViewById(R.id.camera)
val captureButton = findViewById<View>(R.id.button_capture) as Button
val imageView = findViewById<ImageView>(R.id.myImageView)
captureButton.setOnClickListener {
cameraKitView!!.captureImage { _, capturedImage ->
val savedPhoto = getOutputMediaFile()
try {
val outputStream = FileOutputStream(savedPhoto!!.path)
outputStream.write(capturedImage)
outputStream.close()
val myBitmap = BitmapFactory.decodeFile(savedPhoto.getAbsolutePath())
imageView.setImageBitmap(myBitmap)
} catch (e: java.io.IOException) {
e.printStackTrace()
}
}
}
}
这是我的布局:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.camerakit.CameraKitView
android:id="@+id/camera"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:adjustViewBounds="true"/>
<Button
android:id="@+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/myImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0.5"
android:adjustViewBounds="true"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/app_name" />
</android.support.constraint.ConstraintLayout>
但是当显示图像并想要拍摄另一张图像时,cameraview 不像 imageview 那样缩放
已解决:
只需将 scaleType 更改为 CentreCrop 。这就对了