Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.terrylinla.rnsketchcanvas;

import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand All @@ -9,6 +10,8 @@
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
Expand All @@ -24,6 +27,8 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;

class CanvasText {
Expand Down Expand Up @@ -52,6 +57,7 @@ public class SketchCanvas extends View {
private int mOriginalWidth, mOriginalHeight;
private Bitmap mBackgroundImage;
private String mContentMode;
private Uri backgroundURI;

private ArrayList<CanvasText> mArrCanvasText = new ArrayList<CanvasText>();
private ArrayList<CanvasText> mArrTextOnSketch = new ArrayList<CanvasText>();
Expand All @@ -68,14 +74,27 @@ public boolean openImageFile(String filename, String directory, String mode) {
filename.lastIndexOf('.') == -1 ? filename : filename.substring(0, filename.lastIndexOf('.')),
"drawable",
mContext.getPackageName());
File file = new File(filename, directory == null ? "" : directory);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
Bitmap bitmap = res == 0 ?
BitmapFactory.decodeFile(new File(filename, directory == null ? "" : directory).toString(), bitmapOptions) :
Bitmap bitmap = res == 0 ?
BitmapFactory.decodeFile(file.toString(), bitmapOptions) :
BitmapFactory.decodeResource(mContext.getResources(), res);
if(bitmap != null) {
mBackgroundImage = bitmap;
mOriginalHeight = bitmap.getHeight();
mOriginalWidth = bitmap.getWidth();
backgroundURI = Uri.fromFile(file);
ExifInterface exif = null;
try {
exif = new ExifInterface(backgroundURI.getPath());
} catch (IOException e) {
e.printStackTrace();
}

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

Bitmap newBitmap = rotateBitmap(bitmap, orientation);

mBackgroundImage = newBitmap;
mOriginalHeight = newBitmap.getHeight();
mOriginalWidth = newBitmap.getWidth();
mContentMode = mode;

invalidateCanvas(true);
Expand Down Expand Up @@ -336,6 +355,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);


if (mNeedsFullRedraw && mDrawingCanvas != null) {
mDrawingCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.MULTIPLY);
for(SketchData path: mPaths) {
Expand All @@ -345,10 +365,12 @@ protected void onDraw(Canvas canvas) {
}

if (mBackgroundImage != null) {

Rect dstRect = new Rect();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(mBackgroundImage, null,
Utility.fillImage(mBackgroundImage.getWidth(), mBackgroundImage.getHeight(), dstRect.width(), dstRect.height(), mContentMode),

canvas.drawBitmap(mBackgroundImage, null,
Utility.fillImage(mBackgroundImage.getWidth(), mBackgroundImage.getHeight(), dstRect.width(), dstRect.height(), mContentMode),
null);
}

Expand All @@ -357,6 +379,7 @@ protected void onDraw(Canvas canvas) {
}

if (mDrawingBitmap != null) {

canvas.drawBitmap(mDrawingBitmap, 0, 0, mPaint);
}

Expand All @@ -381,11 +404,67 @@ private void invalidateCanvas(boolean shouldDispatchEvent) {
invalidate();
}

private static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bmRotated;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}


private Bitmap createImage(boolean transparent, boolean includeImage, boolean includeText, boolean cropToImageSize) {
Bitmap bitmap = Bitmap.createBitmap(
mBackgroundImage != null && cropToImageSize ? mOriginalWidth : getWidth(),
mBackgroundImage != null && cropToImageSize ? mOriginalHeight : getHeight(),
Bitmap.Config.ARGB_8888);

// if background is not null, then rotate it
if (backgroundURI != null){
ExifInterface exif = null;
try {
exif = new ExifInterface(backgroundURI.getPath());
} catch (IOException e) {
e.printStackTrace();
}

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

Bitmap newBitmap = rotateBitmap(bitmap, orientation);
Comment on lines +457 to +466
Copy link

@Kichiyaki Kichiyaki Oct 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fragment should be wrapped in the if statement that checks if backgroundURI is null (currently, you can't save an image without background).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, check is added now.

}
Canvas canvas = new Canvas(bitmap);
canvas.drawARGB(transparent ? 0 : 255, 255, 255, 255);

Expand Down