我设计了一款软件,希望可以在拍摄一张图片后,点击图片中的某个点,就给出该点的RGB值。目前在虚拟机中实现的结果如图一所示:能比较好的完成目标,点击空白部分不会有数值改变,即点击空白部分不会有影响;但在实际机测试图二中,并没有实现上部对齐,而是有留白,并且点击上下空白区域时会用RGB值变化,这是异常的情况。想知道应该怎么解决呢?
图一
package com.example.lemon;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
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.TextView;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Application<abc> extends AppCompatActivity {
//全局变量
public static final int TAKE_PHOTO = 1;
private static TextView Lemon_R;
private static TextView Lemon_G;
private static TextView Lemon_B;
private static TextView Lemon_GRGB;
private static double RGB_R;
private static double RGB_B;
private static double RGB_G;
private static int intRGB_R;
private static int intRGB_B;
private static int intRGB_G;
private static double GRGB;
private static Bitmap bitmap;
private static int color;
private static float BitmapX;
private static float BitmapY;
private ImageView picture;
private Uri imageUri;
private ImageView ImageViewCenter;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
//
super.onCreate(savedInstanceState);
setContentView(R.layout.appilication);
//关于Toolbar
Toolbar Lemon_Toolbar = findViewById(R.id.Lemon_Toolbar);
Lemon_Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Application.this, MainActivity.class));
}
});
//组件定义
Button takephoto = findViewById(R.id.take_photo);
picture = findViewById(R.id.picture);
//ImageViewCenter = findViewById(R.id.Lemon_ImageViewCenter);
Lemon_R = findViewById(R.id.Lemon_R);
Lemon_G = findViewById(R.id.Lemon_G);
Lemon_B = findViewById(R.id.Lemon_B);
Lemon_GRGB = findViewById(R.id.Lemon_GRGB);
//takephoto按钮
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对象,并把它显现出
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
picture.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
BitmapX = motionEvent.getX();
BitmapY = motionEvent.getY();
SetText();
return false;
}
});
//颜色信息显示
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
//位置选取函数
//颜色显示函数
public static void SetText() {
//获取中心点的颜色信息
//BitmapY=bitmap.getHeight();
//BitmapX=bitmap.getWidth();
int IntBitmapX =(int) BitmapX;
int IntBitmapY =(int) BitmapY;
color = bitmap.getPixel(IntBitmapX,IntBitmapY);//x、y为bitmap所对应的位置
//计算与转换
RGB_R=Color.red(color);
RGB_G=Color.green(color);
RGB_B=Color.blue(color);
intRGB_R=Color.red(color);
intRGB_G=Color.green(color);
intRGB_B=Color.blue(color);
GRGB=RGB_G/(RGB_R+RGB_G+RGB_B);
String result = String .format("%.4f",GRGB);
//颜色显示
//red
Lemon_R.setText("R值:" + RGB_R);//会覆盖text中设置的
Lemon_R.setTextColor(Color.rgb(255,255,255));
Lemon_R.setBackgroundColor(Color.rgb(intRGB_R,0,0));
//green
Lemon_G.setText("G值:" + RGB_G);
Lemon_G.setTextColor(Color.rgb(255,255,255));
Lemon_G.setBackgroundColor(Color.rgb(0,intRGB_G,0));
//blue
Lemon_B.setText("B值:" + RGB_B);
Lemon_B.setTextColor(Color.rgb(255,255,255));
Lemon_B.setBackgroundColor(Color.rgb(0,0,intRGB_B));
//personal
Lemon_GRGB.setText("G/RGB值:" + result);
Lemon_GRGB.setTextColor(Color.rgb(255,255,255));
Lemon_GRGB.setBackgroundColor(color);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/Lemon_Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
app:title="Lemon"
app:titleTextColor="@color/white"
app:subtitle="Text"
app:subtitleTextColor="@color/black"
android:background="@color/purple_200">
<TextView
android:text="APPLICATION"
android:textColor="@color/white"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="@+id/Lemon_R"
android:text="@string/R"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Lemon_Toolbar"
/>
<TextView
android:id="@+id/Lemon_G"
android:text="@string/G"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/Lemon_Toolbar"
android:layout_toRightOf="@+id/Lemon_R"
/>
<TextView
android:id="@+id/Lemon_B"
android:text="@string/B"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Lemon_Toolbar"
android:layout_toRightOf="@+id/Lemon_G"
/>
<TextView
android:id="@+id/Lemon_GRGB"
android:text="@string/GRGB"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Lemon_G"
/>
<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="take photo"
android:layout_below="@+id/Lemon_GRGB"
/>
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/take_photo"
android:layout_centerHorizontal="true" />
</RelativeLayout>