Skip to content

Instantly share code, notes, and snippets.

@Ayyagaries
Created April 1, 2019 15:03
Show Gist options
  • Save Ayyagaries/b49b886d544f6a9cf7fc9fe3aad7a94e to your computer and use it in GitHub Desktop.
Save Ayyagaries/b49b886d544f6a9cf7fc9fe3aad7a94e to your computer and use it in GitHub Desktop.
Java file in my module
/**
* This file was auto-generated by the Titanium Module SDK helper for Android
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2017 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*
*/
package com.example.ble;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import android.graphics.Bitmap;
import java.nio.ByteBuffer;
import android.graphics.Color;
@Kroll.module(name="BLEModule", id="com.example.ble")
public class BLEModuleModule extends KrollModule
{
// Standard Debugging variables
private static final String LCAT = "BLEModuleModule";
private static final boolean DBG = TiConfig.LOGD;
// You can define constants with @Kroll.constant, for example:
// @Kroll.constant public static final String EXTERNAL_NAME = value;
public BLEModuleModule()
{
super();
}
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(LCAT, "inside onAppCreate");
// put module init code that needs to run when the application is created
}
// Methods
@Kroll.method
public String example()
{
Log.d(LCAT, "example called");
return "hello world";
}
// Properties
@Kroll.getProperty
public String getExampleProp()
{
Log.d(LCAT, "get example property");
return "hello world";
}
//This what I am trying to do.
// Properties
@Kroll.getProperty
public byte[] getByteArrayFromBitmap(Bitmap bitmap)
{
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0,
bitmap.getWidth(), bitmap.getHeight());
ByteBuffer eightByteBuffer = ByteBuffer.allocate(pixels.length);
for (int i = 0; i < pixels.length; i++) {
int p = pixels[i];
int red = Color.red(p);
int green = Color.green(p);
int blue = Color.blue(p);
byte p1Rgb = (byte) (((int) Math.round(red / 255.0 * 7.0) << 5) |
((int) Math.round(green / 255.0 * 7.0) << 2) |
((int) Math.round(blue / 255.0 * 3.0)));
eightByteBuffer.put(p1Rgb);
}
return eightByteBuffer.array();
}
@Kroll.setProperty
public void setExampleProp(String value) {
Log.d(LCAT, "set example property: " + value);
}
}
@ayyagary
Copy link

ayyagary commented Apr 1, 2019

i am getting error here

@Kroll.getProperty
public byte[] getByteArrayFromBitmap(Bitmap bitmap)
{
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0,
bitmap.getWidth(), bitmap.getHeight());

    ByteBuffer eightByteBuffer = ByteBuffer.allocate(pixels.length);

    for (int i = 0; i < pixels.length; i++) {
        int p = pixels[i];

        int red = Color.red(p);
        int green = Color.green(p);
        int blue = Color.blue(p);

        byte p1Rgb = (byte) (((int) Math.round(red / 255.0 * 7.0) << 5) |
                ((int) Math.round(green / 255.0 * 7.0) << 2) |
                ((int) Math.round(blue / 255.0 * 3.0)));

        eightByteBuffer.put(p1Rgb);
    }


    return eightByteBuffer.array();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment