【问题标题】:could not be resolved ANDROID_LOG_DEBUG in android无法在android中解决ANDROID_LOG_DEBUG
【发布时间】:2012-06-01 10:20:12
【问题描述】:

我想以编程方式截取屏幕截图。我已经参加了以下课程 http://www.pocketmagic.net/?p=1473,但不截图。 当我尝试使用调试代码时调试代码

 __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,"messsage");

它无法识别 ANDROID_LOG_DEBUG 在此符号“ANDROID_LOG_DEBUG”无法解析给我错误而是我包含了 android/log.h

请帮忙解决这个问题。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <time.h>
#include <linux/fb.h>
#include <linux/kd.h>
#include <jni.h>
#include "pixelflinger.h"
#include<sys/syscall.h>
#include<sys/stat.h>
#include <string.h>
#include <android/log.h>

typedef struct {
     long filesize;
     char reserved[2];
     long headersize;
     long infoSize;
     long width;
     long depth;
     short biPlanes;
     short bits;
     long biCompression;
     long biSizeImage;
     long biXPelsPerMeter;
     long biYPelsPerMeter;
     long biClrUsed;
     long biClrImportant;
} BMPHEAD;

//copyright text
char cprght[255]="Copyright(C)2009 Motisan Radu , All rights reserved.\n radu.motisan@gmail.com";
//surface pointer
static GGLSurface gr_framebuffer[2];
//handler
static int gr_fb_fd = -1;
//v screen info
static struct fb_var_screeninfo vi;
//f screen info
struct fb_fix_screeninfo fi;

static void dumpinfo(struct fb_fix_screeninfo *fi,
                     struct fb_var_screeninfo *vi);

static int get_framebuffer(GGLSurface *fb)
{
    int fd;
    void *bits;

    fd = open("/dev/graphics/fb0", O_RDWR);
    if(fd < 0) {
        perror("cannot open fb0");
        return -1;
    }

    if(ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
        perror("failed to get fb0 info");
        return -1;
    }

    if(ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
        perror("failed to get fb0 info");
        return -1;
    }

    //dumpinfo(&fi, &vi);

    bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(bits == MAP_FAILED) {
        perror("failed to mmap framebuffer");
        return -1;
    }

    fb->version = sizeof(*fb);
    fb->width = vi.xres;
    fb->height = vi.yres;
    fb->stride = fi.line_length / (vi.bits_per_pixel >> 3);
    fb->data = bits;
    fb->format = GGL_PIXEL_FORMAT_RGB_565;

    fb++;

    fb->version = sizeof(*fb);
    fb->width = vi.xres;
    fb->height = vi.yres;
    fb->stride = fi.line_length / (vi.bits_per_pixel >> 3);
    fb->data = (void*) (((unsigned) bits) + vi.yres * vi.xres * 2);
    fb->format = GGL_PIXEL_FORMAT_RGB_565;

    return fd;
}


static void dumpinfo(struct fb_fix_screeninfo *fi, struct fb_var_screeninfo *vi)
{
    /*fprintf(stderr,"vi.xres = %d\n", vi->xres);
    fprintf(stderr,"vi.yres = %d\n", vi->yres);
    fprintf(stderr,"vi.xresv = %d\n", vi->xres_virtual);
    fprintf(stderr,"vi.yresv = %d\n", vi->yres_virtual);
    fprintf(stderr,"vi.xoff = %d\n", vi->xoffset);
    fprintf(stderr,"vi.yoff = %d\n", vi->yoffset);
    fprintf(stderr, "vi.bits_per_pixel = %d\n", vi->bits_per_pixel);

    fprintf(stderr, "fi.line_length = %d\n", fi->line_length);*/

}


//int main(int argc, char **argv)
jint Java_com_example_JazzkActivity_stringFromJNI( JNIEnv* env, jobject thiz )
//int main(int argc, char **argv)
{

  __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,"messsage");
 //get screen capture
  gr_fb_fd = get_framebuffer(gr_framebuffer);
  if (gr_fb_fd <= 0) exit(1);
  //__android_log_print(ANDROID_API_LEVEL_H);
  int w = vi.xres, h = vi.yres, depth = vi.bits_per_pixel;

  //convert pixel data
  uint8_t *rgb24;
  if (depth == 16)
  {
    rgb24 = (uint8_t *)malloc(w * h * 3);
    int i = 0;
    for (;i<w*h;i++)
    {
        uint16_t pixel16 = ((uint16_t *)gr_framebuffer[0].data)[i];
        // RRRRRGGGGGGBBBBBB -> RRRRRRRRGGGGGGGGBBBBBBBB
        // in rgb24 color max is 2^8 per channel (*255/32 *255/64 *255/32)
        rgb24[3*i+2]   = (255*(pixel16 & 0x001F))/ 32;      //Blue
        rgb24[3*i+1]   = (255*((pixel16 & 0x07E0) >> 5))/64;    //Green
        rgb24[3*i]     = (255*((pixel16 & 0xF800) >> 11))/32;   //Red
    }
  }
  else
  if (depth == 24) //exactly what we need
  {
    rgb24 = (uint8_t *) gr_framebuffer[0].data;
  }
  else
  if (depth == 32) //skip transparency channel
  {
      rgb24 = (uint8_t *) malloc(w * h * 3);
      //rgb24 = new uint8_t[w * h * 3];
    int i=0;
    for (;i<w*h;i++)
    {
        uint32_t pixel32 = ((uint32_t *)gr_framebuffer[0].data)[i];
        // in rgb24 color max is 2^8 per channel
        rgb24[3*i+0]   =  pixel32 & 0x000000FF;         //Blue
        rgb24[3*i+1]   = (pixel32 & 0x0000FF00) >> 8;   //Green
        rgb24[3*i+2]   = (pixel32 & 0x00FF0000) >> 16;  //Red
    }
  }
  else
  {
    //free
        close(gr_fb_fd);
    exit(2);
  };
  //save RGB 24 Bitmap
  int bytes_per_pixel = 3;
  BMPHEAD bh;
  memset ((char *)&bh,0,sizeof(BMPHEAD)); // sets everything to 0
  //bh.filesize  =   calculated size of your file (see below)
  //bh.reserved  = two zero bytes
  bh.headersize  = 54L;         // for 24 bit images
  bh.infoSize  =  0x28L;        // for 24 bit images
  bh.width     = w;         // width of image in pixels
  bh.depth     = h;         // height of image in pixels
  bh.biPlanes  =  1;            // for 24 bit images
  bh.bits      = 8 * bytes_per_pixel;   // for 24 bit images
  bh.biCompression = 0L;        // no compression
  int bytesPerLine;
  bytesPerLine = w * bytes_per_pixel;   // for 24 bit images
  //round up to a dword boundary
  if (bytesPerLine & 0x0003)
  {
        bytesPerLine |= 0x0003;
        ++bytesPerLine;
  }
  bh.filesize = bh.headersize + (long)bytesPerLine * bh.depth;
  FILE * bmpfile;
  //printf("Bytes per line : %d\n", bytesPerLine);
  bmpfile = fopen("screen.bmp", "wb");
  if (bmpfile == NULL)
  {
    close(gr_fb_fd);
    //exit(3);
  }
  fwrite("BM",1,2,bmpfile);
  fwrite((char *)&bh, 1, sizeof (bh), bmpfile);
  //fwrite(rgb24,1,w*h*3,bmpfile);
  char *linebuf;
  linebuf = (char *) calloc(1, bytesPerLine);
  if (linebuf == NULL)
  {
        fclose(bmpfile);
    close(gr_fb_fd);
    exit(4);
  }
  int line,x;
  for (line = h-1; line >= 0; line --)
  {
        // fill line linebuf with the image data for that line
    for( x =0 ; x < w; x++ )
    {
        *(linebuf+x*bytes_per_pixel) = *(rgb24 + (x+line*w)*bytes_per_pixel+2);
        *(linebuf+x*bytes_per_pixel+1) = *(rgb24 + (x+line*w)*bytes_per_pixel+1);
        *(linebuf+x*bytes_per_pixel+2) = *(rgb24 + (x+line*w)*bytes_per_pixel+0);
    }
    // remember that the order is BGR and if width is not a multiple
        // of 4 then the last few bytes may be unused
    fwrite(linebuf, 1, bytesPerLine, bmpfile);
  }
  fclose(bmpfile);
  close(gr_fb_fd);
  return 0;
}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS    := -llog
LOCAL_MODULE    := jazzk
### Add all source file names to be included in lib separated by a whitespace
LOCAL_SRC_FILES := capturescr.c

include $(BUILD_SHARED_LIBRARY)

提前致谢

如果有人知道以编程方式实现屏幕截图的其他方法,请发布。

【问题讨论】:

  • 你在makefile中有这个-llog标志吗?
  • 你需要在make文件中添加代码老虎提到的日志指令
  • 我已经写过given.check问题我已经用android.mk更新了
  • 我没有看到您在该代码中使用ANDROID_LOG_DEBUG,错误源自何处?
  • 我正在用 Java_com_example_JazzkActivity_stringFromJNI 方法写这个,再次查看问题我已经更新它。我正在使用 ecplise 进行开发

标签: android linux android-ndk java-native-interface


【解决方案1】:

我已经构建了你的代码,它在调用 jni 时效果很好。我只是删除了__android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,"messsage") 标签,并在Android 源代码项目中构建。

您只需要将 .so 文件放入您的 Java 项目。我不尝试构建 NDK。

【讨论】:

  • 最后我可以通过添加以下路径来做到这一点 project->properties->C/C++ General->path and symbols->Add 1. C:\eclipse\android-ndk-r8- windows\android-ndk-r8\platforms\android-14\arch-arm\usr\include 2. C:\eclipse\android-ndk-r8-windows\android-ndk-r8\toolchains\arm-linux-androideabi- 4.4.3\prebuilt\windows\lib\gcc\arm-linux-androideabi\4.4.3\include
  • @robben - 删除触发代码是一个非常糟糕的解决方案,因为这样就不会从其预期的功能中受益。正如我们从 Karan 的评论中看到的那样,代码或 NDK 实际上没有任何问题,而问题在于 Eclipse CDT 的配置错误,导致它生成“错误”,不能代表使用 ndk 的实际构建会发生什么-构建。
【解决方案2】:

在最新的 ADT(22.6.2 包,其中包含 Eclipse 4.3.1)、NDK(r9d)中也有同样的问题。 通过擦除整个事情解决了,安装 ADT(22.0.5 bundle from Aug31,Sept4/2013, which has Eclipse 4.2.1), NDK(r9b).

【讨论】:

  • 实际上,即使在此设置中,问题再次出现。清理项目和项目 -> 构建项目(我已禁用自动构建)重新创建了 .so 库,现在所有符号都已正确找到。
猜你喜欢
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
相关资源
最近更新 更多