我想要GridView中的CheckBox,如果数据库中的package com.example.android.imagetotextmobileload;
import android.Manifest;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.drm.DrmStore;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
public class MainActivity extends AppCompatActivity {
EditText mResultEt;
ImageView mPreviewIv;
private static final int CAMERA_REQUEST_CODE = 200;
private static final int STORAGE_REQUEST_CODE = 400;
private static final int IMAGE_PICK_GALLERY_CODE = 1000;
private static final int IMAGE_PICK_CAMERA_CODE = 1001;
String cameraPermission[];
String storagePermission[];
Uri image_uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResultEt = findViewById(R.id.resultEt);
mPreviewIv = findViewById(R.id.imageIv);
//camera permission
cameraPermission = new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
//storage permission
storagePermission = new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE};
}
//actionbar menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//inflate menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//handle actionbar item clicks
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.addImage){
showImageImportDialog();
}
if (id == R.id.settings){
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
private void showImageImportDialog() {
//items to display in dialog
String[] items = {" Camera", " Gallery"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
//set title
dialog.setTitle("Select Image");
dialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0){
//camera option clicked
/*for OS marshmallow and above we need to ask runtime permission for camera and storage*/
if (!checkCameraPermission()){
//camera permission not allowed, request it
requestCameraPermission();
}
else {
//permission allowed, take picture
pickCamera();
}
}
if (which == 1){
//gallery option clicked
if (!checkStoragePermission()){
//Storage permission not allowed, request it
requestStoragePermission();
}
else {
//permission allowed, open picture
pickGallery();
}
}
}
});
dialog.create().show(); //show dialog
}
private void pickGallery() {
//intent to pick image from gallery
Intent intent = new Intent(Intent.ACTION_PICK);
//set intent type to image
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK_GALLERY_CODE);
}
private void pickCamera() {
//intent to take image from camera, it will also be saved to storage to get high quality image
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPic"); //title of the picture
values.put(MediaStore.Images.Media.DESCRIPTION, "Image To Text");
//description
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}
private void requestStoragePermission() {
ActivityCompat.requestPermissions(this, storagePermission, STORAGE_REQUEST_CODE);
}
private boolean checkStoragePermission() {
boolean result = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result;
}
private void requestCameraPermission() {
ActivityCompat.requestPermissions(this, cameraPermission, CAMERA_REQUEST_CODE);
}
private boolean checkCameraPermission() {
/*Check camera permision and return the result
*In order to get high quality image we have to save to external
storage first
* before inserting to image view that's why storage permission will also be required.*/
boolean result = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) == (PackageManager.PERMISSION_GRANTED);
boolean result1 = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result && result1;
}
//handle permission result
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case CAMERA_REQUEST_CODE:
if (grantResults.length >0){
boolean cameraAccepted = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean writeStorageAccepted = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
if (cameraAccepted && writeStorageAccepted){
pickCamera();
}
else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
break;
case STORAGE_REQUEST_CODE:
if (grantResults.length >0){
boolean writeStorageAccepted = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
if (writeStorageAccepted){
pickGallery();
}
else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
//handle image result
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == RESULT_OK){
if (requestCode == IMAGE_PICK_GALLERY_CODE){
//got image from gallery now crop it
CropImage.activity(data.getData())
.setGuidelines(CropImageView.Guidelines.ON) //enable image guidlines
.start(this);
}
if (requestCode == IMAGE_PICK_CAMERA_CODE){
//got image from camera now crop it
CropImage.activity(image_uri)
.setGuidelines(CropImageView.Guidelines.ON) //enable image guidlines
.start(this);
}
}
//get cropped image
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (requestCode == RESULT_OK){
Uri resultUri = result.getUri(); //get image uri
//set image to image view
mPreviewIv.setImageURI(resultUri);
//get drawable bitmap for text recognition
BitmapDrawable bitmapDrawable = (BitmapDrawable)mPreviewIv.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!recognizer.isOperational()){
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = recognizer.detect(frame);
StringBuilder sb = new StringBuilder();
//get text from sb until there is no text
for (int i = 0; i<items.size(); i++){
TextBlock myItem = items.valueAt(i);
sb.append(myItem.getValue());
sb.append("\n");
}
//set text to edit text
mResultEt.setText(sb.toString());
}
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE){
//if there is any error show it.
Exception error = result.getError();
Toast.makeText(this, ""+error, Toast.LENGTH_SHORT).show();
}
}
}
}
是IsApproved
,并且应该相应地更改true
,则应该已经检查过。如果我们取消选中该复选框,那么ForeColor
应该变成IsApproved
。这段代码不起作用。有人可以提供更好的解决方案吗?
false
答案 0 :(得分:1)
在这里,IsApproved
字段是boolean
吗?
如果是,那么您不需要使用条件运算符。
只需使用这种方式:
Checked='<%# Eval("IsApproved") %>'
ForeColor='<%#iif(Eval("IsApproved"),"Green","Red") %>'