AndroidDocumentScanner icon indicating copy to clipboard operation
AndroidDocumentScanner copied to clipboard

App crashed while setting bitmap

Open uzzam-web-dev opened this issue 3 years ago • 2 comments

I am getting the bitmap correctly but when I setImage to documentScanner the app crashed. And the error is NullPointerException

uzzam-web-dev avatar Nov 29 '22 21:11 uzzam-web-dev

your ImagePath is null, fix that it should work then

KamranKhanDemo avatar Sep 25 '23 14:09 KamranKhanDemo

The follwoing code I have written in Java for ImageCropActivity, you can use that if you are using Java, or better to use Already written Kotlin Code `import androidx.appcompat.app.AppCompatActivity;

import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar;

import com.your_project.R;

import com.labters.documentscanner.DocumentScannerView;

import kotlin.Unit;

public class ImageCropActivity extends AppCompatActivity {

private static final String FILE_DIR = "FileDir";
DocumentScannerView documentScannerView;

ImageView resultImage;
ProgressBar progressBar;
Button btnImageCrop;

private static ImageCroppedCallBack callback;

public static Intent newIntent(Context context, String selectedFilePath, ImageCroppedCallBack imageCroppedCallBack) {
    Intent intent = new Intent(context, ImageCropActivity.class);
    intent.putExtra(FILE_DIR, selectedFilePath);
    //intent.putExtra("callback",callback);
    callback = imageCroppedCallBack;

    return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_crop);
    documentScannerView = findViewById(R.id.document_scanner);
    btnImageCrop = findViewById(R.id.btnImageCrop);
    progressBar = findViewById(R.id.progressBar);
    resultImage = findViewById(R.id.result_image);

    String filePath = getIntent().getStringExtra(FILE_DIR);
    Bitmap bitmap = assetToBitmap(filePath);

    //ImageProcessingUtils.processImage(filePath, filePath);

    LoadListenerFunction onLoadListener = new LoadListenerFunction() {
        @Override
        public Unit invoke(Boolean loading) {
            // Your implementation here
            if (loading) {
                // Handle loading state
            } else {
                // Handle not loading state
            }
            progressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
            return Unit.INSTANCE;
        }
    };

    documentScannerView.setOnLoadListener(onLoadListener);

    documentScannerView.setImage(bitmap);

    btnImageCrop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);
            // Simulate coroutine behavior using a separate thread or AsyncTask
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Bitmap image = documentScannerView.getCroppedImage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            resultImage.setVisibility(View.VISIBLE);
                            resultImage.setImageBitmap(image);
                            callback.onImageCropped(image);
                            finish();
                        }
                    });
                }
            }).start();
        }
    });
}

private Bitmap assetToBitmap(String file) {
    try {

        return BitmapFactory.decodeFile(file);
       

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

} `

KamranKhanDemo avatar Sep 25 '23 14:09 KamranKhanDemo