我需要帮助whit kotlin,我需要捕获并在我的媒体商店中保存图像 我的代码:
class MainActivity : AppCompatActivity() {
var ListadeProductos= ArrayList<Notas>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
camera.setOnClickListener{
val intentCamera= Intent("android.media.action.IMAGE_CAPTURE")
startActivity(intentCamera)
}
}
}
答案 0 :(得分:1)
我想也许下一个代码可以帮助您从摄像头捕获图像并将其显示在ImageView中。我用过它,(在https://www.youtube.com/watch?v=5wbeWN4hQt0找到了它)
val CAMERA_REQUEST_CODE=0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener{
val callCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if(callCameraIntent.resolveActivity(packageManager)!=null){
startActivityForResult(callCameraIntent,CAMERA_REQUEST_CODE)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
CAMERA_REQUEST_CODE ->{
if(resultCode==Activity.RESULT_OK && data !=null){
imageView.setImageBitmap(data.extras.get("data") as Bitmap)
}
}
else -> {
Toast.makeText(this,"Unrecognized request code",Toast.LENGTH_SHORT)
}
}
}
答案 1 :(得分:1)
最简单的代码是打开本地相机应用程序以拍照并使用OnActivityResult方法处理结果,如文章 Capture Picture with Camera in Kotlin – Android 中所示。
val REQUEST_CODE = 200
fun capturePhoto() {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE && data != null){
imageView.setImageBitmap(data.extras.get("data") as Bitmap)
}
}
就是这样。
答案 2 :(得分:0)
完整代码以选择图像并对其进行裁剪
步骤1 :-添加此gradle
implementation 'com.theartofdev.edmodo:android-image-cropper:1.2.1'
第2步:-在您的包和清单中添加两个活动
<activity android:name=".ui.activities.CropImageActivity"
android:label="Crop Image"></activity>
<activity android:name=".ui.activities.ImagePickerActivity"
android:label="Pick Image"></activity>
CropImageActivity类
public class CropImageActivity extends AppCompatActivity implements CropImageView.OnSetImageUriCompleteListener, CropImageView.OnGetCroppedImageCompleteListener {
private static final int DEFAULT_ASPECT_RATIO_VALUES = 100;
public static final String CROPPED_IMAGE_PATH = "cropped_image_path";
public static final String EXTRA_IMAGE_URI = "cropped_image_path";
public static final String FIXED_ASPECT_RATIO = "extra_fixed_aspect_ratio";
public static final String EXTRA_ASPECT_RATIO_X = "extra_aspect_ratio_x";
public static final String EXTRA_ASPECT_RATIO_Y = "extra_aspect_ratio_y";
private static final String ASPECT_RATIO_X = "ASPECT_RATIO_X";
private static final String ASPECT_RATIO_Y = "ASPECT_RATIO_Y";
private CropImageView mCropImageView;
private int mAspectRatioX = DEFAULT_ASPECT_RATIO_VALUES;
private int mAspectRatioY = DEFAULT_ASPECT_RATIO_VALUES;
private boolean isFixedAspectRatio = false;
Bitmap croppedImage;
//endregion
private TextView tv_cancel,tv_crop;
// Saves the state upon rotating the screen/restarting the activity
@Override
protected void onSaveInstanceState(@SuppressWarnings("NullableProblems") Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt(ASPECT_RATIO_X, mAspectRatioX);
bundle.putInt(ASPECT_RATIO_Y, mAspectRatioY);
}
// Restores the state upon rotating the screen/restarting the activity
@Override
protected void onRestoreInstanceState(@SuppressWarnings("NullableProblems") Bundle bundle) {
super.onRestoreInstanceState(bundle);
mAspectRatioX = bundle.getInt(ASPECT_RATIO_X);
mAspectRatioY = bundle.getInt(ASPECT_RATIO_Y);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_image);
tv_cancel=(TextView)findViewById(R.id.tv_cancel);
tv_crop=(TextView)findViewById(R.id.tv_crop);
if (!getIntent().hasExtra(EXTRA_IMAGE_URI)) {
cropFailed();
return;
}
isFixedAspectRatio = getIntent().getBooleanExtra(FIXED_ASPECT_RATIO, false);
mAspectRatioX = getIntent().getIntExtra(EXTRA_ASPECT_RATIO_X, DEFAULT_ASPECT_RATIO_VALUES);
mAspectRatioY = getIntent().getIntExtra(EXTRA_ASPECT_RATIO_Y, DEFAULT_ASPECT_RATIO_VALUES);
Uri imageUri = Uri.parse(getIntent().getStringExtra(EXTRA_IMAGE_URI));
// Initialize components of the app
mCropImageView = (CropImageView) findViewById(R.id.CropImageView);
// If you want to fix the aspect ratio, set it to 'true'
mCropImageView.setFixedAspectRatio(isFixedAspectRatio);
if (savedInstanceState == null) {
mCropImageView.setImageUriAsync(imageUri);
}
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cropFailed();
}
});
tv_crop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCropImageView.getCroppedImageAsync(mCropImageView.getCropShape(), 0, 0);
}
});
}
private void cropFailed() {
Toast.makeText(mCropImageView.getContext(), "Image crop failed", Toast.LENGTH_LONG).show();
setResult(RESULT_CANCELED);
finish();
}
@Override
protected void onStart() {
super.onStart();
mCropImageView.setOnSetImageUriCompleteListener(this);
mCropImageView.setOnGetCroppedImageCompleteListener(this);
}
@Override
protected void onStop() {
super.onStop();
mCropImageView.setOnSetImageUriCompleteListener(null);
mCropImageView.setOnGetCroppedImageCompleteListener(null);
}
@Override
public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
if (error == null) {
//Toast.makeText(mCropImageView.getContext(), "Image load successful", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(mCropImageView.getContext(), "Image load failed: " + error.getMessage(), Toast.LENGTH_LONG).show();
Toast.makeText(mCropImageView.getContext(), "Unable to load image", Toast.LENGTH_LONG).show();
}
}
@Override
public void onGetCroppedImageComplete(CropImageView view, Bitmap bitmap, Exception error) {
if (error == null) {
croppedImage = bitmap;
try {
String path = saveToInternalStorage(this, bitmap);
Intent resultIntent = new Intent();
resultIntent.putExtra(CROPPED_IMAGE_PATH, path);
setResult(Activity.RESULT_OK, resultIntent);
finish();
} catch (IOException e) {
e.printStackTrace();
cropFailed();
}
} else {
cropFailed();
}
}
private String saveToInternalStorage(Context context, Bitmap bitmapImage) throws IOException {
ContextWrapper cw = new ContextWrapper(context);
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath = new File(directory, "image.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
//Bitmap scaledBitmap = getCompressedBitmap(bitmapImage);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 70, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
fos.close();
}
return directory.getAbsolutePath();
}
}
ImagePickerActivity类
public class ImagePickerActivity extends AppCompatActivity {
private static final int REQUEST_PICK_IMAGE = 2365;
private static final int REQUEST_CROP_IMAGE = 2342;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivityForResult(getPickImageChooserIntent(), REQUEST_PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if(requestCode == REQUEST_PICK_IMAGE) {
Intent intent = new Intent(this, CropImageActivity.class);
Uri imageUri = getPickImageResultUri(data);
intent.putExtra(CropImageActivity.EXTRA_IMAGE_URI, imageUri.toString());
startActivityForResult(intent, REQUEST_CROP_IMAGE);
}
else if(requestCode == REQUEST_CROP_IMAGE) {
System.out.println("Image crop success :"+data.getStringExtra(CropImageActivity.CROPPED_IMAGE_PATH));
String imagePath = new File(data.getStringExtra(CropImageActivity.CROPPED_IMAGE_PATH), "image.jpg").getAbsolutePath();
Intent result = new Intent();
result.putExtra("image_path", imagePath);
setResult(Activity.RESULT_OK, result);
finish();
}
}
else {
System.out.println("Image crop failed");
setResult(Activity.RESULT_CANCELED);
finish();
}
}
/**
* Create a chooser intent to select the source to get image from.<br/>
* The source can be camera's (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/>
* All possible sources are added to the intent chooser.
*/
public Intent getPickImageChooserIntent() {
// Determine Uri of camera image to save.
Uri outputFileUri = getCaptureImageOutputUri();
List<Intent> allIntents = new ArrayList<>();
PackageManager packageManager = getPackageManager();
// collect all camera intents
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
}
allIntents.add(intent);
}
// collect all gallery intents
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
for (ResolveInfo res : listGallery) {
Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
allIntents.add(intent);
}
// the main intent is the last in the list, so pickup the useless one
Intent mainIntent = allIntents.get(allIntents.size() - 1);
for (Intent intent : allIntents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
mainIntent = intent;
break;
}
}
allIntents.remove(mainIntent);
// Create a chooser from the main intent
Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
// Add all other intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));
return chooserIntent;
}
/**
* Get URI to image received from capture by camera.
*/
private Uri getCaptureImageOutputUri() {
Uri outputFileUri = null;
File getImage = getExternalCacheDir();
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
}
return outputFileUri;
}
/**
* Get the URI of the selected image from {@link #getPickImageChooserIntent()}.<br/>
* Will return the correct URI for camera and gallery image.
*
* @param data the returned data of the activity result
*/
public Uri getPickImageResultUri(Intent data) {
boolean isCamera = true;
if (data != null) {
String action = data.getAction();
isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
return isCamera ? getCaptureImageOutputUri() : data.getData();
}
}
activity_crop_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/colorblue"
android:padding="15dp">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Cancel"
android:textColor="@color/colorwhite"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv_title"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="Crop"
android:textColor="@color/colorwhite"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<com.theartofdev.edmodo.cropper.CropImageView
android:id="@+id/CropImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header"
android:layout_gravity="center"
app:cropFixAspectRatio="true" />
</RelativeLayout>
第3步:-在片段或活动类中的用法
private val REQUEST_PICK_IMAGE = 1002
profile_pic1.setOnClickListener {
startActivityForResult(Intent(activity, ImagePickerActivity::class.java), REQUEST_PICK_IMAGE)
}
fun setImage(imagePath: String) {
profile_pic1.setImageBitmap(getImageFromStorage(imagePath));
}
fun getImageFromStorage(path: String): Bitmap {
var f = File(path);
var options = BitmapFactory.Options();
options.inJustDecodeBounds = false;
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 512, 512);
return BitmapFactory.decodeStream(FileInputStream(f), null, options)
}
private fun calculateInSampleSize(
options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
// Raw height and width of image
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
val halfHeight = height / 2
val halfWidth = width / 2
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while (halfHeight / inSampleSize > reqHeight && halfWidth / inSampleSize > reqWidth) {
inSampleSize *= 2
}
}
return inSampleSize
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == requestCode) {
var imagePath = data!!.getStringExtra("image_path");
setImage(imagePath);
} else {
System.out.println("Failed to load image");
}
}
注意:-清单中的DECLARE WRITE / READ和CAMERA权限,以及 以及运行时权限
答案 3 :(得分:0)
this 博客解释了如何在 android/kotlin 中从相机拍摄照片并在 imagview 中显示 -- 非常简单