android jpeg库的简单使用

总结:

android源码包里面已经移植了jpeg库,生成了libjpeg.so

调试工程中我主要是Android.mk没有写对,半天编译不通过

附上成功的代码,其实是抄袭别人的代码,不管那么多,先调通再说!


LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= jpeg_test.c

LOCAL_MODULE:= jtest

#LOCAL_FORCE_STATIC_EXECUTABLE := true

#LOCAL_STATIC_LIBRARIES := libc 
LOCAL_SHARED_LIBRARIES:= libjpeg libc
LOCAL_C_INCLUDES := $(LOCAL_PATH)

LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := debug

include $(BUILD_EXECUTABLE)

#include <string.h>  
#include <stdlib.h>
//#include <jni.h>  

#include <math.h>  
#include <stdio.h>  
#include <stdint.h>  

typedef uint8_t BYTE;  
#define true 1  
#define false 0  


#include "jpeglib.h"


int generateJPEG(BYTE* data,int w, int h, const char* outfilename)  
{  
	int nComponent = 3;  

	struct jpeg_compress_struct jcs;  

	struct jpeg_error_mgr jem;  

	jcs.err = jpeg_std_error(&jem);   
	jpeg_create_compress(&jcs);  

	FILE* f=fopen(outfilename,"wb");  
	if (f==NULL)   
	{  
		free(data);  
		return 0;  
	}  
	jpeg_stdio_dest(&jcs, f);  
	jcs.image_width = w;             
	jcs.image_height = h;  
	jcs.input_components = nComponent;          
	if (nComponent==1)  
		jcs.in_color_space = JCS_GRAYSCALE; 
	else   
		jcs.in_color_space = JCS_RGB;  

	jpeg_set_defaults(&jcs);      
	jpeg_set_quality (&jcs, 60, true);  

	jpeg_start_compress(&jcs, TRUE);  

	JSAMPROW row_pointer[1];         
	int row_stride;                    

	row_stride = jcs.image_width*nComponent;       


	while (jcs.next_scanline < jcs.image_height) {  
		row_pointer[0] = & data[jcs.next_scanline*row_stride];  

		jpeg_write_scanlines(&jcs, row_pointer, 1);  
	}  

	jpeg_finish_compress(&jcs);  
	jpeg_destroy_compress(&jcs);  
	fclose(f);  

	return 1;  
}  


BYTE*  generateRGB24Data()  
{  
	struct {  
		BYTE r;  
		BYTE g;  
		BYTE b;  
	} pRGB[100][199]; 

	memset( pRGB, 0, sizeof(pRGB) ); // ÉèÖñ³Ÿ°ÎªºÚÉ«  

	int i=0, j=0;  

	for(  i=50;i<70;i++ ){  
		for( j=70;j<140;j++ ){  
			pRGB[i][j].b = 0xff;  
		}  
	}  


	for(  i=0;i<10;i++ ){  
		for( j=0;j<199;j++ ){  
			pRGB[i][j].r = 0xff;  
		}  
	}  

	BYTE* ret = (BYTE*)malloc(sizeof(BYTE)*100*199*3);  
	memcpy(ret, (BYTE*)pRGB, sizeof(pRGB));  
	return ret;  
}  

int main(void)
{  
	BYTE* data = generateRGB24Data();  
	generateJPEG(data,199, 100, "/sdcard/test.jpg");  

	free(data);  
	return 0;
}

在工程的目录下要放入几个头文件:

jtest/

|--Android.mk

|--jconfig.h

|--jmorecfg.h

|--jpeglib.h

|--jpeg_test.c


至于代码问题研读一下jpeg源码中的example.c就可以了,学来学去还是在学别人的工具怎么用,

在android的external下面还有好多第三方工程,比如libpng等,有机会用一下!


你可能感兴趣的:(android,struct,Module,byte,Components,math.h)