官方给的文档里面有详细的显示截图的代码 网址如下 https://gitee.com/mirrors_cocos-creator/example-cases/tree/v2.4.3/assets/cases/07_capture_texture
摄像机截图就是截取摄像机当前显示的画面
我的代码为
capture_scree.ts
@property(cc.Camera)
camera: cc.Camera
@property(cc.Node)
jietu: cc.Node
@property(cc.Node)
jietu2: cc.Node
@property(cc.Node)
jietu3: cc.Node
_canvas: null
_width: 0
_height: 0
texture
picData: Uint8Array;
start() {
this.init1();
// create the capture
this.camera.enabled = true;
this.jietu3.active = false;
this.schedule(() => {
this.jietu3.active = true;
let picData = this.initImage();
this.createCanvas(picData);
this.label.string = 'Showing the capture'
this.saveFile(picData)
this.camera.enabled = false;
}, 1, 0);
}
init1() {
this.label.string = '';
let texture = new cc.RenderTexture();
texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, cc.gfx.RB_FMT_S8);
this.camera.targetTexture = texture;
this.texture = texture;
}
// override
initImage() {
let data = this.texture.readPixels();
this._width = this.texture.width;
this._height = this.texture.height;
let picData = this.filpYImage(data, this._width, this._height);
this.picData = picData
return picData;
}
// override init with Data
createCanvas(picData) {
let width = cc.winSize.width;
let height = cc.winSize.height;
let texture = new cc.Texture2D();
texture.initWithData(picData, 32, this._width, this._height);
let spriteFrame = new cc.SpriteFrame();
spriteFrame.setTexture(texture);
let node = this.jietu;
let sprite = node.getComponent(cc.Sprite);
sprite.spriteFrame = spriteFrame;
// node.x = width / 2;
// node.y = height / 2;
this.jietu2.width = width * 0.8
this.jietu2.height = height * 0.8
this.jietu.width = width * 0.6
this.jietu.height = height * 0.6
this.jietu3.x *= 0.6
this.jietu3.y *= 0.6
}
saveFile(picData) {
console.log("进到这里了22222")
if (CC_JSB) {
console.log("进到这里了11111")
let filePath = jsb.fileUtils.getWritablePath() + 'render_to_sprite_image.png';
let success = jsb.saveImageData(picData, this._width, this._height, filePath)
console.log(success, "successsuccesssuccess")
if (success) {
console.log("save image data success, file: " + filePath)
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "saveTextureToLocal2", "(Ljava/lang/String;)V",filePath);
//jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "saveTextureToLocal", "(Ljava/lang/String;)V", filePath);
}
else {
console.log("save image data failed!")
// cc.error("save image data failed!");
}
}
}
// This is a temporary solution
filpYImage(data, width, height) {
// create the data array
let picData = new Uint8Array(width * height * 4);
console.log("11111", picData)
let rowBytes = width * 4;
for (let row = 0; row < height; row++) {
let srow = height - 1 - row;
let start = srow * width * 4;
let reStart = row * width * 4;
// save the piexls data
for (let i = 0; i < rowBytes; i++) {
picData[reStart + i] = data[start + i];
}
}
console.log("222222", picData)
return picData;
}
captureAction(capture, width, height) {
let scaleAction = cc.scaleTo(1, 0.3);
let targetPos = cc.v2(width - width / 6, height / 4);
let moveAction = cc.moveTo(1, targetPos);
let spawn = cc.spawn(scaleAction, moveAction);
capture.runAction(spawn);
let blinkAction = cc.blink(0.1, 1);
// scene action
this.node.runAction(blinkAction);
}
比较需要注意的是内部保存成功后需要把图片变成相册中的照片 就需要Java那边做操作了 ??jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",?"saveTextureToLocal2",?"(Ljava/lang/String;)V",filePath);
调用这个静态方法 把我们内部储存好的图片路径传进去 ?
下面是saveTextureToLocal2方法 ?
// 获取 路径中的图片 保存到本地
public static void saveTextureToLocal2( String pngPath) {
//先调用上边的方法 获取一下权限 有的时候会说你没有权限
//从路径中读取 照片
Bitmap bmp = BitmapFactory.decodeFile(pngPath);
// fileName ==textureName 尽量和JS保存的一致
String fileName = "textureName";
File file = new File(pngPath);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Log.d("保存成功",pngPath );
} catch (FileNotFoundException e) {
Log.d("保存错误1",e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("保存错误2",e.toString());
e.printStackTrace();
}
// 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(AppActivity.getContext().getApplicationContext().getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
AppActivity.getContext().getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));
}
下面是引用方法 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这样就能成功截图咯
|