android怎么用摄像头扫描感知扫描区域的大概颜色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android怎么用摄像头扫描感知扫描区域的大概颜色相关的知识,希望对你有一定的参考价值。

参考技术A 第一步,将图片缩小,再整个过程中,可以降低计算量和减少内存的使用,跟不缩小也能达到一样的效果

/**
* Scale the bitmap down so that it's smallest dimension is
* @value #CALCULATE_BITMAP_MIN_DIMENSIONpx. If @code bitmap is smaller than this, than it
* is returned.
*/
private static Bitmap scaleBitmapDown(Bitmap bitmap)
final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());
if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION)
// If the bitmap is small enough already, just return it
return bitmap;

final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension;
return Bitmap.createScaledBitmap(bitmap,
Math.round(bitmap.getWidth() * scaleRatio),
Math.round(bitmap.getHeight() * scaleRatio),
false);


第二步,将缩小后的图片数据,放在一个int 数组里

/**
* Factory-method to generate a @link ColorCutQuantizer from a @link Bitmap object.
*
* @param bitmap Bitmap to extract the pixel data from
* @param maxColors The maximum number of colors that should be in the result palette.
*/
static ColorCutQuantizer fromBitmap(Bitmap bitmap, int maxColors)
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
return new ColorCutQuantizer(new ColorHistogram(pixels), maxColors);


第三步,将这个int 数组由小到大排序,就相当于,将一张图片一样的颜色堆在一起,然后计算共有多少种颜色,每种颜色它是多大,这些是在一个叫ColorHistogram(颜色直方图)类里面计算的,用颜色直方图来说,就是共有多少柱颜色,每柱颜色有多高

/**
* Class which provides a histogram for RGB values.
*/
final class ColorHistogram
private final int[] mColors;
private final int[] mColorCounts;
private final int mNumberColors;
/**
* A new @link ColorHistogram instance.
*
* @param pixels array of image contents
*/
ColorHistogram(final int[] pixels)
// Sort the pixels to enable counting below
Arrays.sort(pixels);
// Count number of distinct colors
mNumberColors = countDistinctColors(pixels);
// Create arrays
mColors = new int[mNumberColors];
mColorCounts = new int[mNumberColors];
// Finally count the frequency of each color
countFrequencies(pixels);

/**
* @return 获取共用多少柱不同颜色 number of distinct colors in the image.
*/
int getNumberOfColors()
return mNumberColors;

/**
* @return 获取排好序后的不同颜色的数组 an array containing all of the distinct colors in the image.
*/
int[] getColors()
return mColors;

/**
* @return 获取保存每一柱有多高的数组 an array containing the frequency of a distinct colors within the image.
*/
int[] getColorCounts()
return mColorCounts;

//计算共用多少柱不同颜色
private static int countDistinctColors(final int[] pixels)
if (pixels.length < 2)
// If we have less than 2 pixels we can stop here
return pixels.length;

// If we have at least 2 pixels, we have a minimum of 1 color...
int colorCount = 1;
int currentColor = pixels[0];
// Now iterate from the second pixel to the end, counting distinct colors
for (int i = 1; i < pixels.length; i++)
// If we encounter a new color, increase the population
if (pixels[i] != currentColor)
currentColor = pixels[i];
colorCount++;


return colorCount;


//计算每一柱有多高
private void countFrequencies(final int[] pixels)
if (pixels.length == 0)
return;

int currentColorIndex = 0;
int currentColor = pixels[0];
mColors[currentColorIndex] = currentColor;
mColorCounts[currentColorIndex] = 1;
Log.i(pixels.length,+ pixels.length);

if (pixels.length == 1)
// If we only have one pixel, we can stop here
return;


// Now iterate from the second pixel to the end, population distinct colors
for (int i = 1; i < pixels.length; i++)
if (pixels[i] == currentColor)
// We've hit the same color as before, increase population
mColorCounts[currentColorIndex]++;
else
// We've hit a new color, increase index
currentColor = pixels[i];
currentColorIndex++;
mColors[currentColorIndex] = currentColor;
mColorCounts[currentColorIndex] = 1;





第四步,将各种颜色,根据RGB转HSL算法,得出对应的HSL(H: Hue 色相,S:Saturation 饱和度L Lightness 明度),根据特定的条件,比如是明度L是否接近白色,黑色,还有一个判断叫isNearRedILine,解释是@return true if the color lies close to the red side of the I line(接近红色私密区域附近?).,然后根据这三个条件,过滤掉这些颜色,什么是HSL和RGB转HSL算法可以查看下百科,比较有详细说明

/**
* Private constructor.
*
* @param colorHistogram histogram representing an image's pixel data
* @param maxColors The maximum number of colors that should be in the result palette.
*/
private ColorCutQuantizer(ColorHistogram colorHistogram, int maxColors)
final int rawColorCount = colorHistogram.getNumberOfColors();
final int[] rawColors = colorHistogram.getColors();//颜色数组
final int[] rawColorCounts = colorHistogram.getColorCounts();//对应rawColors每一个颜色数组的大小
// First, lets pack the populations into a SparseIntArray so that they can be easily
// retrieved without knowing a color's index
mColorPopulations = new SparseIntArray(rawColorCount);
for (int i = 0; i < rawColors.length; i++)
mColorPopulations.append(rawColors[i], rawColorCounts[i]);

// Now go through all of the colors and keep those which we do not want to ignore
mColors = new int[rawColorCount];
int validColorCount = 0;
for (int color : rawColors)
if (!shouldIgnoreColor(color))
mColors[validColorCount++] = color;


Log.d(mColors length, +mColors.length);
if (validColorCount <= maxColors)
// The image has fewer colors than the maximum requested, so just return the colors
mQuantizedColors = new ArrayList();

for (final int color : mColors)
mQuantizedColors.add(new Swatch(color, mColorPopulations.get(color)));

else
// We need use quantization to reduce the number of colors
mQuantizedColors = quantizePixels(validColorCount - 1, maxColors);

Android中的Altbeacon扫描仪特定UUID

【中文标题】Android中的Altbeacon扫描仪特定UUID【英文标题】:Altbeacon scanner specific UUID in Android 【发布时间】:2019-08-15 15:46:32 【问题描述】:

如果我需要监控 UUID 的特定区域。

在一个区域中声明,但是当进入 didEnterRegion 时,我只得到 UUID 和 Major 和 Minor 为空,我是否需要做 didRangeBeaconsInRegion 来只找到定义的区域而不找到另一个不需要的区域?

我将区域定义如下:

Region region = new Region ("region", Identifier.parse ("UUID"), null, null);

另一个问题,图书馆是否按位置查找信标?

非常感谢, 问候

public void onBeaconServiceConnect() 

    beaconManager.addMonitorNotifier(new MonitorNotifier() 
    @Override
    public void didEnterRegion(Region region) 
        Log.i(TAG, " enter region");
        try 
            beaconManager.startRangingBeaconsInRegion(region);
            Log.i(TAG, "region beacon" + region.getId1() + " " + region.getId2() + " " + region.getId3());
         catch (RemoteException e) 
            e.printStackTrace();
        
    

    @Override
    public void didExitRegion(Region region) 
        Log.i(TAG, "I no longer see an beacon");
        try 
            beaconManager.stopRangingBeaconsInRegion(region);
         catch (RemoteException e) 
            e.printStackTrace();
        
    

    @Override
    public void didDetermineStateForRegion(int state, Region region) 
        Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+ state);
    

);

try 
      beaconManager.startMonitoringBeaconsInRegion(new 
Region("myMonitoringUniqueId", Identifier.parse("UUID"), null, null));
     catch (RemoteException e) 

   

【问题讨论】:

【参考方案1】:

didEnterRegion 回调不会告诉您哪个特定信标与您的区域定义匹配。它返回您在调用startMonitoringBeaconsInRegion 时使用的Region 对象的副本。如果您想获取匹配信标的特定标识符,请使用startRangingBeaconsInRegion(...) 并等待回调到didRangeBeaconsInRegion(...),其中将包含所有匹配信标的列表。

API 以这种方式工作的原因有很多,但最根本的原因是该 API 模仿 iOS 上等效的 CoreLocation API 以实现互操作性,并且它们的工作方式相同。

【讨论】:

以上是关于android怎么用摄像头扫描感知扫描区域的大概颜色的主要内容,如果未能解决你的问题,请参考以下文章

手机怎么扫描摄像头?

html5怎么扫描二维码

如何使摄像头扫描二维码,然后解析二维码 java源码 zxing

wps的pdf扫描识别异常是啥原因

js + html5 + java 实现二维码和条形码扫描.怎么用js调用摄像头

Android实现二维码扫描登录网页