已经成功通过代码完成了拍照并显示,现在希望可以获取图片中某点的RGB值
package com.example.lemon;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.util.EventLog.Event;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Application<abc> extends AppCompatActivity {
//全局变量
public static final int TAKE_PHOTO = 1;
private ImageView picture;
private Uri imageUri;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appilication);
Button takephoto = findViewById(R.id.take_photo);
picture = findViewById(R.id.picture);
takephoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
try//判断图片是否存在,存在则删除在创建,不存在则直接创建
{
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//判断运行设备的系统版本是否低于Android7.0
if (Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(Application.this,
"com.example.Lemon.fileprovider", outputImage);
} else {
imageUri = Uri.fromFile(outputImage);
}
//使用隐示的Intent,调用摄像头,并把它存储
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
//调用会返回结果的开启方式,返回成功的话,则把它显示出来
}
});
}
@SuppressLint("MissingSuperCall")
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
//将图片解析成Bitmap对象,并把它显现出
//
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
######结果如图所示