Re: How can copy SkBitmap draw to android.graphics.Bitmap?

346 views
Skip to first unread message

Derek Sollenberger

unread,
Apr 25, 2017, 1:35:20 PM4/25/17
to skia-discuss
I haven't looked into why the getPixels call is returning null, but in your case you shouldn't be allocating a separate native bitmap, but should instead wrap the existing pixels.
You can wrap the pixels by calling SkSurface::MakeRasterDirect which will return a surface that you can surface->getCanvas().  This will allow you to draw directly into the pixels allocated by the Java bitmap.

On Tue, Apr 25, 2017 at 12:09 AM Mingyue 郭 <gmybi...@gmail.com> wrote:
In Android Java:

public class SkiaDrawView extends View {
public Bitmap fSkiaBitmap;

public SkiaDrawView(Context ctx, AttributeSet attr) {
super(ctx, attr);
}

@Override
public void onSizeChanged(int w, int h, int oldw, int oldh)
{
fSkiaBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
}

@Override
public void onDraw(Canvas canvas)
{
fun1(fSkiaBitmap);
canvas.drawBitmap(fSkiaBitmap, 0, 0, null);

}
    
    public native void fun1(Bitmap bitmap);
}

Jni:
JNIEXPORT void JNICALL Java_com_example_viewdraw_viewdrawinndkskiausestaticlib_SkiaDrawView_fun1(JNIEnv * env, jobject obj, jobject dstBitmap)
{
AndroidBitmapInfo dstInfo;
AndroidBitmap_getInfo(env, dstBitmap, &dstInfo);

//draw SkBitmap;
SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(dstInfo.width, dstInfo.height);
SkBitmap bitmap;
bitmap.allocPixels(imageInfo);
// if(!bitmap.tryAllocPixels(imageInfo))
// {
// __android_log_print(ANDROID_LOG_INFO, "YimaLib_ViewDraw", "bitmap.tryAllocPixels faild...");
// return;
// }
SkCanvas canvas(bitmap);
canvas.drawColor(SK_ColorRED);

    void *dstPixels;
AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels);
//bitmap.lockPixels(); //when use this: build error:undefined reference to 'SkBitmap::lockPixels() const'
memcpy(dstPixels, bitmap.getPixels(), dstInfo.width*dstInfo.height*4); // the bitmap.getPixels() is null, I want copying bitmap to dstBitmap, what I should do ?
AndroidBitmap_unlockPixels(env, dstBitmap);
}

I want draw something in SkBitmap in C++, and then show in Android View Use android.graphics.Bitmap.



--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages